forked from FreePBX/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dashboard.class.php
336 lines (290 loc) · 9.31 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
<?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;
}
// 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 $regen = 100;
// Keep this number of days worth of system stats
private $history = 90;
// IF you're adding a new builtin hook, add the descriptive name
// and size and everything to classes/DashboardHooks, too.
private $builtinhooks = array(
"overview" => "getAjaxOverview",
"blog" => "getBlogPosts",
"notifications" => "getAjaxNotifications",
"sysstat" => "getAjaxSysStat",
"aststat" => "getAjaxAsteriskStat",
"uptime" => "getAjaxUptime",
"srvstat" => "getAjaxServerStats",
"registered" => "getRegoInfo",
// This is for testing, and isn't used. If you remove it, tests
// will fail.
"fake" => "fake",
);
// This is our scheduler task. It should run every minute.
private $sched = "scheduler.php";
public function install() {
$this->freepbx->Config->remove_conf_settings(array("DASHBOARD_INFO_UPDATE_TIME", "MAXCALLS", "DASHBOARD_STATS_UPDATE_TIME"));
}
public function uninstall() {
}
public function backup() {
}
public function restore($backup) {
}
public function runTests($db) {
return true;
}
public function doConfigPageInit() {
}
public function myDialplanHooks() {
return true;
}
public function doDialplanHook(&$ext, $engine, $priority) {
// We're not actually doing any dialplan modifications. This
// is just a handy place to discover modules that have requested hooks
// into the status page.
if (!class_exists('DashboardHooks')) {
include 'classes/DashboardHooks.class.php';
}
$allhooks = DashboardHooks::genHooks($this->getConfig('visualorder'));
$this->setConfig('allhooks', $allhooks);
// Also, while we're here, we should check that our cronjob is
// still there.
$file = \FreePBX::Config()->get('AMPWEBROOT')."/admin/modules/dashboard/".$this->sched;
$cmd = "[ -x $file ] && $file";
// Ensure we instantiate cron with the correct user
$c = \FreePBX::Cron(\FreePBX::Config()->get('AMPASTERISKWEBUSER'));
$c->addLine("* * * * * $cmd");
}
public function ajaxRequest($req, &$setting) {
return true;
}
/**
* Chown hook for freepbx fwconsole
*/
public function chownFreepbx() {
$files = array();
$files[] = array('type' => 'file', 'path' => __DIR__."/scheduler.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 array("status" => true);
break;
case "resetmessage":
\FreePBX::create()->Notifications->reset($_REQUEST['raw'], $_REQUEST['id']);
return array("status" => true);
break;
case "saveorder":
$this->setConfig('visualorder',$_REQUEST['order']);
return array("status" => true);
break;
case "getcontent":
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 array("status" => true, "content" => $class->getContent($_REQUEST['section']));
} else {
return array("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 = array();
foreach($page['entries'] as $k => $e) {
$o = isset($order[$e['section']]) ? $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 runTrigger() {
// This is run every minute.
$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->setConfig('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();
$p->doPrune();
}
public function getSysInfo() {
$si = $this->getConfig('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($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 = array(
"ok" => "glyphicon-ok text-success",
"warning" => "glyphicon-warning-sign text-warning",
"error" => "glyphicon-remove text-danger",
"unknown" => "glyphicon-question-sign text-info",
"info" => "glyphicon-info-sign text-info",
"critical" => "glyphicon-fire text-danger"
);
// Are we being asked for an alert we actually know about?
if (!isset($glyphs[$res])) {
return array('type' => 'unknown', "tooltip" => "Don't know what $res is", "glyph-class" => $glyphs['unknown']);
}
if ($tt === null) {
// No Tooltip
return array('type' => $res, "tooltip" => null, "glyph-class" => $glyphs[$res]);
} else {
// Generate a tooltip
$html = '';
if (is_array($tt)) {
foreach ($tt as $line) {
$html .= htmlentities(\ForceUTF8\Encoding::fixUTF8($line), ENT_QUOTES,"UTF-8")."\n";
}
} else {
$html .= htmlentities(\ForceUTF8\Encoding::fixUTF8($tt), ENT_QUOTES,"UTF-8");
}
return array('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 getAjaxAsteriskStat() {
if (!class_exists('Statistics')) {
include 'classes/Statistics.class.php';
}
$s = new Statistics();
return $s->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 getAjaxServerStats() {
return "No\n";
}
public function extIgnoreList(){
$numbers = array();
$hooks = \FreePBX::Hooks()->processHooks();
foreach ($hooks as $key => $value) {
if(is_array($value)){
$numbers = array_merge($numbers,$value);
}
}
return $numbers;
}
}