-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsystemprofile.php
executable file
·167 lines (136 loc) · 3.52 KB
/
systemprofile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
System Profile plugin for the PHP Fat-Free Framework
Can be used with Nagios or F3's throttling feature, amongst other things.
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
Copyright (c) 2010-2011 Killsaw
Steven Bredenberg <[email protected]>
@package SystemProfile
@version 1.0.0
**/
//! Plugin for retrieving information about the current system.
class SystemProfile {
//! Minimum framework version required to run
const F3_Minimum='1.4.0';
//! Treshold for determining if 5-minute system load average is too high.
const OVERLOADED_THRESHOLD = 2.0;
//@{
//! Locale-specific error/exception messages
const
TEXT_NoWindows='Sorry, this class does not work with Windows.';
//@}
/**
Check that this system is not Windows. Only works for *nix.
@protected
**/
protected static function checkOS() {
if (preg_match('/^(Cyg|Win)/', PHP_OS)) {
trigger_error(self::TEXT_NoWindows);
}
}
/**
Get system load levels (1, 5, and 15 minute load avgs)
@return array
@param $userid string
@public
**/
public static function getServerInfo() {
self::checkOS();
return array(
'os' => php_uname('s'),
'hostname' => php_uname('n'),
'release' => php_uname('r'),
'version' => php_uname('v'),
'machine' => php_uname('m'),
);
}
/**
Returns current system's hostname.
@return string
@public
**/
public static function getHostname()
{
return php_uname('n');
}
/**
Parse and return `uptime` info. All or some.
@return array|string
@param $all bool Return all uptime info or just actual uptime value?
@public
**/
public static function getUptime($all=false) {
self::checkOS();
$orig_uptime = `uptime`;
list($sys_time, $info) = explode(' ', $orig_uptime, 2);
$info_parts = explode(', ', $info);
// Some rather nasty string manipulations.
$load = substr($info_parts[3], 15);
$users = intval($info_parts[2]);
$uptime = substr($info_parts[0], 3).$info_parts[1].' hours';
$data = array(
'system_time'=>$sys_time,
'uptime'=>$uptime,
'users'=>$users,
'load'=>$load
);
if (!$all) {
return $data['uptime'];
} else {
return $data;
}
}
/**
Get system load levels (1, 5, and 15 minute load avgs)
@return array
@public
**/
public static function getLoadLevels() {
self::checkOS();
$info = self::getUptime($all=true);
$parts = explode(' ', $info['load']);
return array(
'1m_avg'=>(float)$parts[0],
'5m_avg'=>(float)$parts[1],
'15m_avg'=>(float)$parts[2]
);
}
/**
Get list of logged in users.
@return array
@public
**/
public static function getOnlineUsers() {
self::checkOS();
$lines = explode("\n", trim(`who`));
$users = array();
foreach($lines as $line) {
$parts = preg_split("/\s+/", $line);
$users[] = array(
'user'=>$parts[0],
'term'=>$parts[1],
'date'=>sprintf('%s %s', $parts[2], $parts[3]),
'time'=>$parts[4],
'host'=>isset($parts[5])?substr($parts[5], 1, -1):null
);
}
return $users;
}
/**
Check if system load is too high.
@param $check string
@return bool
@public
**/
public static function systemIsOkay($check='5m_avg') {
$load = self::getLoadLevels();
if ($load[$check] >= self::OVERLOADED_THRESHOLD) {
return false;
} else {
return true;
}
}
}