-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDashboard.class.php
383 lines (334 loc) · 12.2 KB
/
Dashboard.class.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
// vim: set ai ts=4 sw=4 ft=php:
// License for all code of this FreePBX module can be found in the license file inside the module directory
// Copyright 2013 Schmooze Com Inc.
//
class Dashboard extends FreePBX_Helpers implements BMO {
public function __construct($freepbx) {
$this->db = $freepbx->Database;
$this->freepbx = $freepbx;
$this->maxage = $this->freepbx->Config->get('SYS_STATS_MAXAGE');
if ($this->maxage < 50) {
$this->maxage = 50;
}
}
// Always regen sys stats if they're older or equal to this, in seconds.
private $maxage = 50;
// If I've been polled, regen stats if it's been more than this
// number of times the length of time it took to generate it (eg,
// if it took .1 seconds, and this is set to 100, it'll regen the
// status every 10 seconds (or if it took .2, every 20 seconds)
// This is to avoid extra load on the server when it's not needed.
private int $regen = 100;
// Keep this number of days worth of system stats
private int $history = 90;
// IF you're adding a new builtin hook, add the descriptive name
// and size and everything to classes/DashboardHooks, too.
private array $builtinhooks = [
"overview" => "getAjaxOverview",
"blog" => "getBlogPosts",
"notifications" => "getAjaxNotifications",
"sysstat" => "getAjaxSysStat",
"aststat" => "getAjaxAsteriskStat",
"uptime" => "getAjaxUptime",
"srvstat" => "getAjaxServerStats",
"registered" => "getRegoInfo",
"notepad_save" => "saveNote",
"notepad_del" => "delNote",
// This is for testing, and isn't used. If you remove it, tests
// will fail.
"fake" => "fake",
];
public function install() {
$this->freepbx->Config->remove_conf_settings([ "DASHBOARD_INFO_UPDATE_TIME", "MAXCALLS", "DASHBOARD_STATS_UPDATE_TIME" ]);
$this->freepbx->Config->define_conf_setting('SYS_STATS_DISABLE', [ 'value' => false, 'defaultval' => false, 'readonly' => false, 'hidden' => false, 'level' => 0, 'module' => 'dashboard', 'category' => 'Dashboard Module', 'emptyok' => false, 'sortorder' => 1, 'name' => 'Disable collection of system statistics', 'description' => 'Set this to true to prevent persistent collection of system statistics such as CPU, memory, and channel usage.', 'type' => CONF_TYPE_BOOL ], true);
$this->freepbx->Config->define_conf_setting('SYS_STATS_MAXAGE', [ 'value' => 50, 'defaultval' => 50, 'readonly' => false, 'hidden' => false, 'level' => 0, 'options' => [ 50, 86400 ], 'module' => 'dashboard', 'category' => 'Dashboard Module', 'emptyok' => false, 'sortorder' => 1, 'name' => 'Expiry time for system statistics', 'description' => 'Set the maximum age in seconds before system statistics are refreshed. The minimum value is 50 seconds.', 'type' => CONF_TYPE_INT ], true);
$this->freepbx->Config->define_conf_setting('VIEW_FW_STATUS', [ 'value' => true, 'defaultval' => true, 'readonly' => false, 'hidden' => false, 'level' => 0, 'module' => 'dashboard', 'category' => 'Dashboard Module', 'emptyok' => false, 'sortorder' => 1, 'name' => 'Display firewall status', 'description' => 'The Dashboard will display a warning when the PBX Firewall is disabled. When this is set to \'no\', the Dashboard warning will be permanently suppressed.', 'type' => CONF_TYPE_BOOL ], true);
$feeds = $this->freepbx->Config->get('RSSFEEDS');
$feeds = str_replace("\r", "", (string) $feeds);
if (!empty($feeds)) {
$feeds = explode("\n", $feeds);
$i = 0;
$urls = [];
foreach ($feeds as $feed) {
$this->setConfig($feed, null, "content");
$this->setConfig($feed, null, "etag");
$this->setConfig($feed, null, "last_modified");
}
}
}
public function uninstall() {
}
public function backup() {
}
public function restore($backup) {
}
public function runTests($db) {
return true;
}
public function __get($var) {
switch ($var) {
case 'cache':
$this->cache = $this->freepbx->Cache->cloneByNamespace('dashboard');
return $this->cache;
break;
}
}
public function doConfigPageInit() {
}
public function myDialplanHooks() {
return true;
}
public function doDialplanHook(&$ext, $engine, $priority) {
// While we're here, we should check that our cronjob is
// still there.
$crons = $this->freepbx->Cron->getAll();
foreach ($crons as $c) {
if (preg_match('/scheduler\.php/', (string) $c, $matches)) {
$this->freepbx->Cron->remove($c);
}
}
$this->freepbx->Job->addClass('dashboard', 'scheduler', \FreePBX\modules\Dashboard\Job::class, '* * * * *');
}
public function ajaxRequest($req, &$setting) {
return true;
}
/**
* Chown hook for freepbx fwconsole
*/
public function chownFreepbx() {
$files = [ [ 'type' => 'file', 'path' => __DIR__ . "/scheduler.php", 'perms' => 0755 ], [ 'type' => 'file', 'path' => __DIR__ . "/netmon.php", 'perms' => 0755 ] ];
return $files;
}
public function ajaxHandler() {
if (!class_exists('DashboardHooks')) {
include 'classes/DashboardHooks.class.php';
}
switch ($_REQUEST['command']) {
case "deletemessage":
\FreePBX::create()->Notifications->safe_delete($_REQUEST['raw'], $_REQUEST['id']);
return [ "status" => true ];
break;
case "resetmessage":
\FreePBX::create()->Notifications->reset($_REQUEST['raw'], $_REQUEST['id']);
return [ "status" => true ];
break;
case "saveorder":
$this->setConfig('visualorder', $_REQUEST['order']);
return [ "status" => true ];
break;
case "getcontent":
# Diskspace graph is comming from sysadmin
if ($_REQUEST['rawname'] == 'Diskspace' && $this->freepbx->Modules->checkStatus("sysadmin") && method_exists($this->freepbx->Sysadmin, 'DashboardGraph')) {
return [ "status" => true, "content" => $this->freepbx->Sysadmin->DashboardGraph()->getContent() ];
}
else {
if (file_exists(__DIR__ . '/sections/' . $_REQUEST['rawname'] . '.class.php')) {
include(__DIR__ . '/sections/' . $_REQUEST['rawname'] . '.class.php');
$class = '\\FreePBX\\modules\\Dashboard\\Sections\\' . $_REQUEST['rawname'];
$class = new $class();
return [ "status" => true, "content" => $class->getContent($_REQUEST['section']) ];
}
else {
return [ "status" => false, "message" => _("Missing Class Object!") ];
}
}
break;
case "gethooks":
if (!$this->getConfig('allhooks')) {
$e = null;
$this->doDialplanHook($e, null, null); // Avoid warnings.
}
$config = $this->getConfig('allhooks');
$order = $this->getConfig('visualorder');
if (is_array($order)) {
foreach ($config as &$page) {
$entries = [];
foreach ($page['entries'] as $k => $e) {
$o = $order[$e['section']] ?? $k;
while (isset($entries[$o])) {
$o++;
}
$entries[$o] = $e;
}
ksort($entries);
$page['entries'] = $entries;
}
}
return $config;
break;
case "sysstat":
if (!class_exists('Statistics')) {
include 'classes/Statistics.class.php';
}
$s = new Statistics();
return $s->getStats();
break;
default:
return DashboardHooks::runHook($_REQUEST['command']);
break;
}
}
public function ajaxCustomHandler() {
switch ($_REQUEST['command']) {
case "netmon":
if (!class_exists('Netmon')) {
include 'classes/Netmon.class.php';
}
$n = new \FreePBX\modules\Dashboard\Netmon();
$n->getLiveStats();
die();
break;
}
}
public function runTrigger() {
// This is run every minute.
if (!$this->freepbx->Config->get('SYS_STATS_DISABLE')) {
$this->getSysInfo();
}
}
public function genSysInfo() {
// PHP SysInfo requires 'php-xml'. If it doesn't exist,
// then we can't really do anything.
if (!class_exists('DOMDocument') || !extension_loaded('mbstring')) {
return false;
}
// Time how long it takes to run
$start = microtime(true);
if (!class_exists('SysInfo')) {
include 'classes/SysInfo.class.php';
}
$si = SysInfo::create();
$info = $si->getSysInfo();
$end = microtime(true);
$delay = (float) $end - $start;
// This is now a float in seconds of how long it took
// to generate the sysinfo.
// Make sure it's a valid number, and it's not too small.
if ($delay < 0.1 || is_nan($delay)) {
$delay = 0.1;
}
$info['generationlength'] = $delay;
$this->cache->save('latestsysinfo', $info);
$this->setConfig($info['timestamp'], $info, 'MINUTES');
$this->pruneSysInfo();
return $info;
}
public function pruneSysInfo() {
if (!class_exists('PruneHistory')) {
include 'classes/PruneHistory.class.php';
}
$p = new PruneHistory($this->freepbx);
$p->doPrune();
}
public function getSysInfo() {
$si = $this->cache->fetch('latestsysinfo');
// Does it need to be updated? Older than $maxage seconds?
if (!$si || $si['timestamp'] + $this->maxage <= time()) {
$si = $this->genSysInfo();
return $si;
}
// Is this older than $regen * generation time? (See header of this file)
$genafter = ($si['generationlength'] * $this->regen) + $si['timestamp'];
// If it is, regenerate. If not, keep using the cached data.
if ($genafter < time()) {
$si = $this->genSysInfo();
}
return $si;
}
public function getSysInfoPeriod($period = null) {
// Return all of Period's SysInfo
if ($period === null) {
throw new Exception("No Period given");
}
$retarr = $this->getAll($period);
return $retarr;
}
// Manage Built in Hooks
public function doBuiltInHook($hookname) {
$funcname = substr((string) $hookname, 8);
if (!isset($this->builtinhooks[$funcname]))
throw new Exception("I was asked for $funcname, but I don't know what it is!");
$methodname = $this->builtinhooks[$funcname];
if (!method_exists($this, $methodname))
throw new Exception("$funcname wants to use $methodname, but it doesn't exist");
return $this->$methodname();
}
public function genStatusIcon($res, $tt = null) {
$glyphs = [ "ok" => "fa-check text-success", "warning" => "fa-exclamation-triangle text-warning", "error" => "fa-times text-danger", "unknown" => "fa-question-circle text-info", "info" => "fa-info-circle text-info", "critical" => "fa-fire text-danger" ];
// Are we being asked for an alert we actually know about?
if (!isset($glyphs[$res])) {
return [ 'type' => 'unknown', "tooltip" => "Don't know what $res is", "glyph-class" => $glyphs['unknown'] ];
}
if ($tt === null) {
// No Tooltip
return [ 'type' => $res, "tooltip" => null, "glyph-class" => $glyphs[$res] ];
}
else {
// Generate a tooltip
$html = '';
if (is_array($tt)) {
foreach ($tt as $line) {
$html .= htmlentities((string) \ForceUTF8\Encoding::fixUTF8($line), ENT_QUOTES, "UTF-8") . "\n";
}
}
else {
$html .= htmlentities((string) \ForceUTF8\Encoding::fixUTF8($tt), ENT_QUOTES, "UTF-8");
}
return [ 'type' => $res, "tooltip" => $html, "glyph-class" => $glyphs[$res] ];
}
return '';
}
// The actual hooks themselves!
private function getAjaxOverview() {
// Autoloaded!
$o = new Overview();
return $o->getHTML();
}
private function getAjaxSysStat() {
if (!class_exists('SysStat')) {
include 'classes/SysStat.class.php';
}
$sysstat = new SysStat();
return $sysstat->getHTML();
}
private function getAjaxUptime() {
if (!class_exists('Uptime')) {
include 'classes/Uptime.class.php';
}
$tmp = new Uptime();
return $tmp->getHTML();
}
private function getRegoInfo() {
$html = "<p>There would be system info here</p><p>If rob had written it</p>\n";
return $html;
}
private function saveNote() {
$content = new \StdClass;
$content->content = substr((string) $_REQUEST["content"], 0, 2048);
$result = $this->setConfig(time(), $content, "notes");
return $result ? _("Saved note") : _("An error occured");
}
private function delNote() {
$result = $this->setConfig($_REQUEST["id"], false, "notes");
return $result ? _("Deleted note") : _("An error occured");
}
private function getAjaxServerStats() {
return "No\n";
}
public function extIgnoreList() {
$numbers = [];
$hooks = \FreePBX::Hooks()->processHooks();
foreach ($hooks as $key => $value) {
if (is_array($value)) {
$numbers = array_merge($numbers, $value);
}
}
return $numbers;
}
public function getdiskspace() {
include_once __DIR__ . '/classes/DiskUsage.class.php';
$obj_diskusage = new \DiskUsage();
return $obj_diskusage->parsedf();
}
}