-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsetup.php
More file actions
538 lines (484 loc) · 19.4 KB
/
Copy pathsetup.php
File metadata and controls
538 lines (484 loc) · 19.4 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
include_once(__DIR__ . '/includes/constants.php');
include_once(__DIR__ . '/includes/arrays.php');
/**
* Registers this plugin's Cacti hooks (navigation breadcrumbs, config
* arrays/menu, poller_bottom, data source replication) and its
* webseer.php/webseer_servers.php/webseer_proxies.php realm, then
* creates the plugin's database tables. Invoked by the Cacti plugin
* framework when the plugin is installed/enabled.
*
* @return void
*/
function plugin_webseer_install() {
api_plugin_register_hook('webseer', 'draw_navigation_text', 'plugin_webseer_draw_navigation_text', 'setup.php');
api_plugin_register_hook('webseer', 'config_arrays', 'plugin_webseer_config_arrays', 'setup.php');
api_plugin_register_hook('webseer', 'poller_bottom', 'plugin_webseer_poller_bottom', 'setup.php');
api_plugin_register_hook('webseer', 'replicate_out', 'webseer_replicate_out', 'setup.php');
api_plugin_register_realm('webseer', 'webseer.php,webseer_servers.php,webseer_proxies.php', __('Web Service Check Admin', 'webseer'), 1);
plugin_webseer_setup_table();
}
/**
* Drops all of this plugin's database tables. Invoked by the Cacti
* plugin framework when the plugin is uninstalled.
*
* @return void
*/
function plugin_webseer_uninstall() {
db_execute('DROP TABLE IF EXISTS plugin_webseer_servers');
db_execute('DROP TABLE IF EXISTS plugin_webseer_servers_log');
db_execute('DROP TABLE IF EXISTS plugin_webseer_urls');
db_execute('DROP TABLE IF EXISTS plugin_webseer_urls_log');
db_execute('DROP TABLE IF EXISTS plugin_webseer_proxies');
db_execute('DROP TABLE IF EXISTS plugin_webseer_processes');
db_execute('DROP TABLE IF EXISTS plugin_webseer_contacts');
}
/**
* Here we will check to ensure everything is configured
*
* Runs any pending database schema upgrades for this plugin. Invoked by
* plugin_webseer_config_arrays() (the 'config_arrays' hook) only when
* the current page is index.php, plugins.php, or webseer.php - not on
* every page load.
*
* @return bool Always true.
*/
function plugin_webseer_check_config() {
// Here we will check to ensure everything is configured
plugin_webseer_upgrade();
return true;
}
/**
* Here we will upgrade to the newest version
*
* Applies version-gated schema migrations (adding the contacts and
* proxies tables, renaming the URL log table, adding compression/
* notify-format/poller-id columns) and updates the recorded plugin
* version/realm file list, based on comparing the installed version
* against the current INFO file version. Called from
* plugin_webseer_check_config(), which is itself only invoked when the
* current page is index.php, plugins.php, or webseer.php.
*
* @return bool Always true.
*
* @global array $config Cacti global configuration array (declared but
* not directly used here).
*/
function plugin_webseer_upgrade() {
// Here we will upgrade to the newest version
global $config;
$info = plugin_webseer_version();
$new = $info['version'];
$old = db_fetch_cell('SELECT version FROM plugin_config WHERE directory="webseer"');
if ($new != $old) {
if (version_compare($old, '1.1', '<')) {
db_execute("CREATE TABLE IF NOT EXISTS `plugin_webseer_contacts` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`user_id` int(12) NOT NULL,
`type` varchar(32) NOT NULL,
`data` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id_type` (`user_id`,`type`),
KEY `type` (`type`),
KEY `user_id` (`user_id`))
ENGINE=InnoDB
COMMENT='Table of WebSeer contacts'");
}
if (version_compare($old, '2.0', '<')) {
db_execute("CREATE TABLE `plugin_webseer_proxies` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT ,
`name` varchar(30) DEFAULT '',
`hostname` varchar(64) DEFAULT '',
`http_port` mediumint(8) unsigned DEFAULT '80',
`https_port` mediumint(8) unsigned DEFAULT '443',
`username` varchar(40) DEFAULT '',
`password` varchar(60) DEFAULT '',
PRIMARY KEY (`id`),
KEY `hostname` (`hostname`),
KEY `name` (`name`))
ENGINE=InnoDB
COMMENT='Holds Proxy Information for Connections'");
if (!db_column_exists('plugins_webseer_urls', 'proxy_server')) {
db_execute('ALTER TABLE plugin_webseer_urls
ADD COLUMN proxy_server int(11) unsigned NOT NULL default "0" AFTER requiresauth');
}
}
if (version_compare($old, '3.0', '<')) {
db_execute('RENAME TABLE `plugin_webseer_url_log` TO `plugin_webseer_urls_log`');
db_execute('ALTER TABLE `plugin_webseer_urls`
ADD COLUMN compression int(3) unsigned NOT NULL default "0" AFTER lastcheck,
ADD COLUMN notify_format int(3) unsigned NOT NULL default "0" AFTER notify_accounts');
db_execute('ALTER TABLE `plugin_webseer_urls_log`
ADD COLUMN compression int(3) unsigned NOT NULL default "0" AFTER lastcheck');
db_execute('ALTER TABLE `plugin_webseer_servers`
ADD COLUMN compression int(3) unsigned NOT NULL default "0" AFTER lastcheck');
db_execute('ALTER TABLE `plugin_webseer_servers_log`
ADD COLUMN compression int(3) unsigned NOT NULL default "0" AFTER lastcheck');
}
if (!db_column_exists('plugin_webseer_urls', 'notify_list')) {
db_execute('ALTER TABLE plugin_webseer_urls ADD COLUMN notify_list int(10) unsigned NOT NULL default "0" AFTER checkcert');
}
if (!db_column_exists('plugin_webseer_urls', 'poller_id')) {
db_execute('ALTER TABLE plugin_webseer_urls ADD COLUMN poller_id int(10) unsigned NOT NULL default "1" AFTER id');
}
if (!db_column_exists('plugin_webseer_processes', 'poller_id')) {
db_execute('ALTER TABLE plugin_webseer_processes ADD COLUMN poller_id int(10) unsigned NOT NULL default "1" AFTER id');
}
db_execute_prepared('UPDATE plugin_config
SET version = ?
WHERE directory = "webseer"',
[$new]);
db_execute_prepared('UPDATE plugin_config SET
version = ?, name = ?, author = ?, webpage = ?
WHERE directory = ?',
[
$info['version'],
$info['longname'],
$info['author'],
$info['homepage'],
$info['name']
]
);
db_execute_prepared('UPDATE plugin_realms
SET file = ?
WHERE file LIKE "%webseer.php%"',
['webseer.php,webseer_servers.php,webseer_proxies.php']);
api_plugin_register_hook('webseer', 'replicate_out', 'webseer_replicate_out', 'setup.php', '1');
}
return true;
}
/**
* Reads this plugin's version/author metadata from its INFO file.
* Invoked by the Cacti plugin framework to display plugin information,
* and called directly by plugin_webseer_upgrade() and
* poller_webseer.php's display_version().
*
* @return array The plugin's INFO file 'info' section (name, version,
* author, etc.).
*
* @global array $config Cacti global configuration array; used to
* locate the plugin's INFO file.
*/
function plugin_webseer_version() {
global $config;
$info = parse_ini_file($config['base_path'] . '/plugins/webseer/INFO', true);
return $info['info'];
}
/**
* Creates all of this plugin's database tables (servers and their check
* log, service check URLs and their check log, running-process
* tracking, notification contacts, HTTP proxies). Called from
* plugin_webseer_install() during plugin installation.
*
* @return void
*/
function plugin_webseer_setup_table() {
db_execute("CREATE TABLE IF NOT EXISTS `plugin_webseer_servers` (
`id` int(11) unsigned NOT NULL auto_increment,
`enabled` char(2) NOT NULL default 'on',
`name` varchar(64) NOT NULL,
`ip` varchar(120) NOT NULL,
`location` varchar(64) NOT NULL,
`lastcheck` timestamp NOT NULL default '0000-00-00',
`compression` int(3) NOT NULL default '0',
`isme` int(11) unsigned NOT NULL default '0',
`master` int(11) unsigned NOT NULL default '0',
`url` varchar(256) NOT NULL,
PRIMARY KEY (`id`),
KEY `location` (`location`,`lastcheck`),
KEY `isme` (`isme`),
KEY `master` (`master`)) ENGINE=InnoDB
COMMENT='Holds WebSeer Server Definitions'");
db_execute("CREATE TABLE IF NOT EXISTS `plugin_webseer_servers_log` (
`id` int(11) unsigned NOT NULL auto_increment,
`server` int(11) unsigned NOT NULL default '0',
`url_id` int(11) unsigned NOT NULL default '0',
`lastcheck` timestamp NOT NULL default '0000-00-00',
`compression` int(3) unsigned NOT NULL default '0',
`result` int(11) unsigned NOT NULL default '0',
`http_code` int(11) unsigned default NULL,
`error` varchar(256) default NULL,
`total_time` double default NULL,
`namelookup_time` double default NULL,
`connect_time` double default NULL,
`redirect_time` double default NULL,
`redirect_count` int(11) unsigned default NULL,
`size_download` int(11) unsigned default NULL,
`speed_download` int(11) unsigned default NULL,
PRIMARY KEY (`id`),
KEY `url_id` (`url_id`),
KEY `lastcheck` (`lastcheck`),
KEY `result` (`result`))
ENGINE=InnoDB
COMMENT='Holds WebSeer Service Check Results'");
db_execute("CREATE TABLE IF NOT EXISTS `plugin_webseer_urls` (
`id` int(11) unsigned NOT NULL auto_increment,
`poller_id` int(11) unsigned NOT NULL default '1',
`enabled` char(2) NOT NULL default 'on',
`type` varchar(32) NOT NULL default 'http',
`display_name` varchar(64) NOT NULL default '',
`url` varchar(256) NOT NULL,
`ip` varchar(120) NOT NULL default '',
`search` varchar(1024) NOT NULL,
`search_maint` varchar(1024) NOT NULL,
`search_failed` varchar(1024) NOT NULL,
`requiresauth` char(2) NOT NULL default '',
`proxy_server` int(11) unsigned NOT NULL default '0',
`checkcert` char(2) NOT NULL default 'on',
`notify_list` int(10) unsigned NOT NULL default '0',
`notify_accounts` varchar(256) NOT NULL,
`notify_extra` varchar(256) NOT NULL,
`notify_format` int(3) unsigned NOT NULL default '0',
`result` int(11) unsigned NOT NULL default '0',
`downtrigger` int(11) unsigned NOT NULL default '3',
`timeout_trigger` int(11) unsigned NOT NULL default '4',
`failures` int(11) unsigned NOT NULL default '0',
`triggered` int(11) unsigned NOT NULL default '0',
`lastcheck` timestamp NOT NULL default '0000-00-00',
`compression` int(3) unsigned NOT NULL default '0',
`error` varchar(256) default NULL,
`http_code` int(11) unsigned default NULL,
`total_time` double default NULL,
`namelookup_time` double default NULL,
`connect_time` double default NULL,
`redirect_time` double default NULL,
`speed_download` int(11) unsigned default NULL,
`size_download` int(11) unsigned default NULL,
`redirect_count` int(11) unsigned default NULL,
`debug` longblob default NULL,
PRIMARY KEY (`id`),
KEY `lastcheck` (`lastcheck`),
KEY `triggered` (`triggered`),
KEY `result` (`result`),
KEY `enabled` (`enabled`))
ENGINE=InnoDB
COMMENT='Holds WebSeer Service Check Definitions'");
db_execute("CREATE TABLE IF NOT EXISTS `plugin_webseer_urls_log` (
`id` int(11) unsigned NOT NULL auto_increment,
`url_id` int(11) unsigned NOT NULL default '0',
`lastcheck` timestamp NOT NULL default '0000-00-00',
`compression` int(3) unsigned NOT NULL default '0',
`result` int(11) unsigned NOT NULL default '0',
`http_code` int(11) unsigned default NULL,
`error` varchar(256) default NULL,
`total_time` double default NULL,
`namelookup_time` double default NULL,
`connect_time` double default NULL,
`redirect_time` double unsigned default NULL,
`redirect_count` int(11) unsigned default NULL,
`size_download` int(11) unsigned default NULL,
`speed_download` int(11) unsigned default NULL,
PRIMARY KEY (`id`),
KEY `url_id` (`url_id`),
KEY `lastcheck` (`lastcheck`),
KEY `result` (`result`))
ENGINE=InnoDB
COMMENT='Holds WebSeer Service Check Logs'");
db_execute("CREATE TABLE IF NOT EXISTS `plugin_webseer_processes` (
`id` bigint unsigned NOT NULL auto_increment,
`poller_id` int(11) unsigned NOT NULL default '1',
`url_id` int(11) unsigned NOT NULL,
`pid` int(11) unsigned NOT NULL,
`time` timestamp default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `pid` (`pid`),
KEY `url_id` (`url_id`),
KEY `time` (`time`))
ENGINE=MEMORY
COMMENT='Holds running process information'");
db_execute("CREATE TABLE IF NOT EXISTS `plugin_webseer_contacts` (
`id` int(12) NOT NULL auto_increment,
`user_id` int(12) NOT NULL,
`type` varchar(32) NOT NULL,
`data` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id_type` (`user_id`,`type`),
KEY `type` (`type`),
KEY `user_id` (`user_id`))
ENGINE=InnoDB
COMMENT='Table of WebSeer contacts'");
db_execute("CREATE TABLE `plugin_webseer_proxies` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT '',
`hostname` varchar(64) DEFAULT '',
`http_port` mediumint(8) unsigned DEFAULT '80',
`https_port` mediumint(8) unsigned DEFAULT '443',
`username` varchar(40) DEFAULT '',
`password` varchar(60) DEFAULT '',
PRIMARY KEY (`id`),
KEY `hostname` (`hostname`),
KEY `name` (`name`))
ENGINE=InnoDB
COMMENT='Holds Proxy Information for Connections'");
}
/**
* Launches a background poller_webseer.php process to run the
* configured service checks. Invoked by the Cacti plugin framework via
* the 'poller_bottom' hook at the end of each poller cycle.
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* resolve the PHP binary and this plugin's poller
* script path.
*/
function plugin_webseer_poller_bottom() {
global $config;
include_once($config['library_path'] . '/database.php');
$command_string = trim(read_config_option('path_php_binary'));
// If its not set, just assume its in the path
if (trim($command_string) == '') {
$command_string = 'php';
}
$extra_args = ' -q ' . $config['base_path'] . '/plugins/webseer/poller_webseer.php';
exec_background($command_string, $extra_args);
}
/**
* Adds this plugin's 'Service Checks' entry to the Management menu, and
* triggers a schema-upgrade check when the currently displayed page is
* exactly index.php, plugins.php, or webseer.php (not the servers or
* proxies pages). Invoked by the Cacti plugin framework via the
* 'config_arrays' hook.
*
* @return void
*
* @global array $menu Cacti's registered admin
* menu; a 'Service Checks'
* entry is added under
* 'Management'.
* @global array $user_auth_realms Reserved/declared for parity
* with other hook
* implementations; not used
* directly here.
* @global array $user_auth_realm_filenames Reserved/declared for parity
* with other hook
* implementations; not used
* directly here.
*/
function plugin_webseer_config_arrays() {
global $menu, $user_auth_realms, $user_auth_realm_filenames;
$menu[__('Management')]['plugins/webseer/webseer.php'] = __('Service Checks', 'webseer');
$files = ['index.php', 'plugins.php', 'webseer.php'];
if (in_array(get_current_page(), $files, true)) {
plugin_webseer_check_config();
}
}
/**
* Adds this plugin's page breadcrumb/navigation entries (service check
* list/edit/save, server list/edit/save, proxy list/edit/save). Invoked
* by the Cacti plugin framework via the 'draw_navigation_text' hook.
*
* @param array $nav Cacti's registered navigation text entries.
*
* @return array The $nav array with this plugin's entries added.
*/
function plugin_webseer_draw_navigation_text($nav) {
$nav['webseer.php:'] = [
'title' => __('WebSeer Service Checks', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer.php',
'level' => '1'
];
$nav['webseer.php:edit'] = [
'title' => __('Service Check Edit', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer.php',
'level' => '1'
];
$nav['webseer.php:save'] = [
'title' => __('Service Check Save', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer.php',
'level' => '1'
];
$nav['webseer_servers.php:'] = [
'title' => __('WebSeer Servers', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer_servers.php',
'level' => '1'
];
$nav['webseer_servers.php:edit'] = [
'title' => __('Server Edit', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer.php',
'level' => '1'
];
$nav['webseer_servers.php:save'] = [
'title' => __('Save Server', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer.php',
'level' => '1'
];
$nav['webseer_proxies.php:'] = [
'title' => __('WebSeer Proxies', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer.php',
'level' => '1'
];
$nav['webseer_proxies.php:edit'] = [
'title' => __('Proxie Edit', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer.php',
'level' => '1'
];
$nav['webseer_proxies.php:save'] = [
'title' => __('Save Proxy', 'webseer'),
'mapping' => 'index.php:',
'url' => 'webseer.php',
'level' => '1'
];
return $nav;
}
/**
* Replicates this plugin's configuration tables (contacts, proxies,
* servers, service check URLs) out to a remote data collector poller.
* Invoked by the Cacti plugin framework via the 'replicate_out' hook
* during data collector replication.
*
* @param array $data Replication context, including 'remote_poller_id',
* 'rcnn_id' (remote connection id), and 'class'
* (replication scope, e.g. 'all').
*
* @return void
*/
function webseer_replicate_out($data) {
$remote_poller_id = $data['remote_poller_id'];
$rcnn_id = $data['rcnn_id'];
$class = $data['class'];
cacti_log('INFO: Replicating for the WebSeer Plugin', false, 'REPLICATE');
$tables = [
'plugin_webseer_contacts',
'plugin_webseer_proxies',
'plugin_webseer_servers',
'plugin_webseer_urls'
];
if ($class == 'all') {
foreach ($tables as $table) {
$tdata = db_fetch_assoc('SELECT * FROM ' . $table);
replicate_out_table($rcnn_id, $tdata, $table, $remote_poller_id);
}
}
return $data;
}