forked from Cacti/plugin_apcupsd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
346 lines (290 loc) · 12.1 KB
/
setup.php
File metadata and controls
346 lines (290 loc) · 12.1 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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2025 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/ |
+-------------------------------------------------------------------------+
*/
function plugin_apcupsd_install() {
api_plugin_register_hook('apcupsd', 'config_arrays', 'apcupsd_config_arrays', 'setup.php');
api_plugin_register_hook('apcupsd', 'config_settings', 'apcupsd_config_settings', 'setup.php');
api_plugin_register_hook('apcupsd', 'poller_bottom', 'apcupsd_poller_bottom', 'setup.php');
api_plugin_register_hook('apcupsd', 'draw_navigation_text', 'apcupsd_draw_navigation_text', 'setup.php');
api_plugin_register_hook('apcupsd', 'replicate_out', 'apcupsd_replicate_out', 'setup.php');
/* hook for table replication */
api_plugin_register_hook('apcupsd', 'replicate_out', 'apcupsd_replicate_out', 'setup.php');
api_plugin_register_realm('apcupsd', 'upses.php', __('Manage UPS\'s', 'apcupsd'), 1);
apcupsd_setup_table();
}
function plugin_apcupsd_uninstall() {
db_execute('DROP TABLE IF EXISTS apcupsd_ups');
db_execute('DROP TABLE IF EXISTS apcupsd_ups_stats');
return true;
}
function plugin_apcupsd_check_config() {
return true;
}
function plugin_apcupsd_upgrade() {
return true;
}
function apcupsd_check_upgrade() {
global $config, $database_default;
include_once($config['library_path'] . '/database.php');
include_once($config['library_path'] . '/functions.php');
$files = array('plugins.php', 'upses.php');
if (isset($_SERVER['PHP_SELF']) && !in_array(basename($_SERVER['PHP_SELF']), $files)) {
return;
}
$info = plugin_apcupsd_version();
$current = $info['version'];
$old = db_fetch_cell("SELECT version FROM plugin_config WHERE directory='apcupsd'");
if ($current != $old) {
if (api_plugin_is_enabled('apcupsd')) {
# may sound ridiculous, but enables new hooks
api_plugin_enable_hooks('apcupsd');
}
db_execute_prepared("UPDATE plugin_config SET
version = ?, name = ?, author = ?, webpage = ?
WHERE directory = ?",
array(
$info['version'],
$info['longname'],
$info['author'],
$info['homepage'],
$info['name']
)
);
if (db_column_exists('apcupsd_ups_stats', 'ups_abmtemp')) {
db_execute('ALTER TABLE apcupsd_ups_stats CHANGE COLUMN ups_abmtemp ups_ambtemp DOUBLE default NULL');
}
if (!db_column_exists('apcupsd_ups_stats', 'ups_master')) {
db_execute('ALTER TABLE apcupsd_ups_stats ADD COLUMN ups_master varchar(128) NOT NULL default "" AFTER ups_name');
}
if (db_column_exists('apcupsd_ups_stats', 'ups_dispsw')) {
db_execute('ALTER TABLE apcuspd_ups_stats CHANGE COLUMN ups_dispsw ups_dipsw VARCHAR(20) NOT NULL default ""');
}
}
}
function apcupsd_poller_bottom() {
global $config;
include_once($config['base_path'] . '/lib/poller.php');
exec_background(read_config_option('path_php_binary'), ' -q ' . $config['base_path'] . '/plugins/apcupsd/poller_apcupsd.php');
}
function apcupsd_setup_table() {
global $config, $database_default;
include_once($config['library_path'] . '/database.php');
db_execute("CREATE TABLE IF NOT EXISTS `apcupsd_ups` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`poller_id` int(10) unsigned DEFAULT 1,
`host_id` int(10) unsigned NOT NULL DEFAULT 0,
`site_id` int(10) unsigned NOT NULL DEFAULT 0,
`type_id` int(10) unsigned NOT NULL DEFAULT 0,
`name` varchar(40) NOT NULL DEFAULT '',
`description` varchar(128) NOT NULL DEFAULT '',
`snmp_version` tinyint(3) unsigned DEFAULT 2,
`snmp_community` varchar(100) NOT NULL DEFAULT '',
`snmp_username` varchar(50) NOT NULL DEFAULT '',
`snmp_password` varchar(50) NOT NULL DEFAULT '',
`snmp_auth_protocol` varchar(6) NOT NULL DEFAULT '',
`snmp_priv_protocol` varchar(6) NOT NULL DEFAULT '',
`snmp_priv_passphrase` varchar(200) NOT NULL DEFAULT '',
`snmp_context` varchar(64) NOT NULL DEFAULT '',
`snmp_engine_id` varchar(64) NOT NULL DEFAULT '',
`snmp_port` tinyint(3) unsigned NOT NULL DEFAULT 161,
`snmp_timeout` int(10) unsigned NOT NULL DEFAULT 2000,
`snmp_skipped` varchar(255) NOT NULL DEFAULT '',
`status` int(10) unsigned NOT NULL DEFAULT 0,
`hostname` varchar(64) NOT NULL DEFAULT '',
`port` int(10) unsigned NOT NULL DEFAULT 3551,
`enabled` char(2) DEFAULT 'on',
`error_message` varchar(255) DEFAULT '',
`last_updated` timestamp NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`))
ENGINE=InnoDB
COMMENT='Monitored UPS Table'");
// APC : 001,036,0854
// DATE : 2022-07-05 11:47:45 -0400
// HOSTNAME : vmhost3
// VERSION : 3.14.14 (31 May 2016) redhat
// UPSNAME : APC1500
// CABLE : USB Cable
// DRIVER : USB UPS Driver
// UPSMODE : Stand Alone
// STARTTIME: 2022-07-04 20:30:55 -0400
// MODEL : Back-UPS BX1500G
// STATUS : ONLINE
// LINEV : 121.0 Volts
// LOADPCT : 12.0 Percent
// BCHARGE : 100.0 Percent
// TIMELEFT : 48.5 Minutes
// MBATTCHG : 5 Percent
// MINTIMEL : 3 Minutes
// MAXTIME : 0 Seconds
// SENSE : Low
// LOTRANS : 88.0 Volts
// HITRANS : 136.0 Volts
// ALARMDEL : 30 Seconds
// BATTV : 27.2 Volts
// LASTXFER : High line voltage
// NUMXFERS : 0
// TONBATT : 0 Seconds
// CUMONBATT: 0 Seconds
// XOFFBATT : N/A
// SELFTEST : NO
// STATFLAG : 0x05000008
// SERIALNO : 3B1050X33233
// BATTDATE : 2021-04-01
// NOMINV : 120 Volts
// NOMBATTV : 24.0 Volts
// NOMPOWER : 865 Watts
// FIRMWARE : 866.L5 .D USB FW:L5
// END APC : 2022-07-05 11:47:47 -0400
db_execute("CREATE TABLE IF NOT exists `apcupsd_ups_stats` (
`ups_id` int(10) unsigned NOT NULL,
`ups_key` varchar(20) not null default '',
`ups_date` timestamp not null default CURRENT_TIMESTAMP,
`ups_hostname` varchar(64) not null default '',
`ups_version` varchar(64) not null default '',
`ups_name` varchar(20) not null default '',
`ups_master` varchar(128) not null default '',
`ups_cable` varchar(20) not null default '',
`ups_driver` varchar(20) not null default '',
`ups_mode` varchar(20) not null default '',
`ups_starttime` timestamp not null default CURRENT_TIMESTAMP,
`ups_mandate` timestamp not null default CURRENT_TIMESTAMP,
`ups_masterupd` timestamp not null default CURRENT_TIMESTAMP,
`ups_xonbatt` timestamp not null default CURRENT_TIMESTAMP,
`ups_laststest` timestamp not null default CURRENT_TIMESTAMP,
`ups_model` varchar(40) not null default '',
`ups_status` varchar(20) not null default '',
`ups_dipsw` varchar(20) not null default '',
`ups_extbatts` int(10) unsigned default null,
`ups_badbatts` int(10) unsigned default null,
`ups_reg1` varchar(20) not null default '',
`ups_reg2` varchar(20) not null default '',
`ups_reg3` varchar(20) not null default '',
`ups_line_voltage` double default null,
`ups_line_fail` varchar(20) not null default '0',
`ups_load_percent` double default null,
`ups_line_frequency` double default null,
`ups_output_voltage` double default null,
`ups_max_line_voltage` double default null,
`ups_min_line_voltage` double default null,
`ups_timeleft` double default null,
`ups_mbattchg` double default null,
`ups_mintimel` double default null,
`ups_maxtime` double default null,
`ups_sense` varchar(20) not null default '',
`ups_lowtrans` double default null,
`ups_hitrans` double default null,
`ups_alarmdel` double default null,
`ups_dlowbatt` varchar(20) not null default '',
`ups_dshutd` varchar(20) not null default '',
`ups_dwake` varchar(20) not null default '',
`ups_battery_status` varchar(60) not null default '',
`ups_battery_charge` double default null,
`ups_battery_voltage` double default null,
`ups_battery_date` varchar(20) not null default '',
`ups_battery_retpct` double default null,
`ups_lastxfer` varchar(40) not null default '',
`ups_numxfers` int(10) unsigned default null,
`ups_tonbatt` int(10) unsigned default null,
`ups_cumonbatt` int(10) unsigned default null,
`ups_xoffbatt` int(10) unsigned default null,
`ups_selftest` varchar(10) not null default '',
`ups_selftest_interval` varchar(20) not null default '',
`ups_statflag` varchar(20) not null default '',
`ups_serialno` varchar(20) not null default '',
`ups_nominal_voltage` double default null,
`ups_nominal_batt_voltage` double default null,
`ups_nominal_power` double default null,
`ups_nominal_output_voltage` double default null,
`ups_ambtemp` double default null,
`ups_humidity` double default null,
`ups_internal_temp` double default null,
`ups_firmware` varchar(40) not null default '',
`ups_end_rec` timestamp not null default CURRENT_TIMESTAMP,
PRIMARY KEY(ups_id))
ENGINE=InnoDB
COMMENT='Monitored UPS Status Table'");
return true;
}
function plugin_apcupsd_version () {
global $config;
$info = parse_ini_file($config['base_path'] . '/plugins/apcupsd/INFO', true);
return $info['info'];
}
function apcupsd_log_valid_event() {
global $action;
$valid = false;
if (read_config_option('apcupsd_enabled') == 'on') {
if (strpos($_SERVER['SCRIPT_NAME'], 'graph_view.php') !== false) {
$valid = false;
} elseif (strpos($_SERVER['SCRIPT_NAME'], 'user_admin.php') !== false &&
isset_request_var('action') && get_nfilter_request_var('action') == 'checkpass') {
$valid = false;
} elseif (strpos($_SERVER['SCRIPT_NAME'], 'plugins.php') !== false) {
if (isset_request_var('mode')) {
$valid = true;
$action = get_nfilter_request_var('mode');
}
} elseif (strpos($_SERVER['SCRIPT_NAME'], 'auth_profile.php') !== false) {
$valid = false;
} elseif (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false) {
$valid = false;
} elseif (strpos($_SERVER['SCRIPT_NAME'], 'auth_changepassword.php') !== false) {
$valid = false;
} elseif (isset($_POST) && sizeof($_POST)) {
$valid = true;
} elseif (isset_request_var('purge_continue')) {
$valid = true;
$action = 'purge';
}
}
return $valid;
}
function apcupsd_config_arrays() {
global $menu;
$menu[__('Management')]['plugins/apcupsd/upses.php'] = __('UPSes', 'webseer');
if (function_exists('auth_augment_roles')) {
auth_augment_roles(__('System Administration'), array('upses.php'));
}
apcupsd_check_upgrade();
}
function apcupsd_config_settings () {
global $tabs, $settings, $item_rows, $apcupsd_retentions;
}
function apcupsd_replicate_out($data) {
global $config;
include_once($config['base_path'] . '/lib/poller.php');
$upsdata = db_fetch_assoc('SELECT * FROM apcupsd_ups');
replicate_out_table($data['rcnn_id'], $upsdata, 'apcupsd_ups', $data['remote_poller_id']);
$upsdata = db_fetch_assoc('SELECT * FROM apcupsd_ups_stats');
replicate_out_table($data['rcnn_id'], $upsdata, 'apcupsd_ups_stats', $data['remote_poller_id']);
return $data;
}
function apcupsd_draw_navigation_text($nav) {
$nav['upses.php:'] = array(
'title' => __('Manage UPSes', 'apcupsd'),
'mapping' => 'index.php:',
'url' => 'upses.php',
'level' => '1'
);
return $nav;
}