1#!/usr/bin/env php
2<?php
3/*
4 +-------------------------------------------------------------------------+
5 | Copyright (C) 2004-2021 The Cacti Group                                 |
6 |                                                                         |
7 | This program is free software; you can redistribute it and/or           |
8 | modify it under the terms of the GNU General Public License             |
9 | as published by the Free Software Foundation; either version 2          |
10 | of the License, or (at your option) any later version.                  |
11 |                                                                         |
12 | This program is distributed in the hope that it will be useful,         |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
15 | GNU General Public License for more details.                            |
16 +-------------------------------------------------------------------------+
17 | Cacti: The Complete RRDtool-based Graphing Solution                     |
18 +-------------------------------------------------------------------------+
19 | This code is designed, written, and maintained by the Cacti Group. See  |
20 | about.php and/or the AUTHORS file for specific developer information.   |
21 +-------------------------------------------------------------------------+
22 | http://www.cacti.net/                                                   |
23 +-------------------------------------------------------------------------+
24*/
25
26error_reporting(0);
27
28if (!isset($called_by_script_server)) {
29	include_once(dirname(__FILE__) . '/../include/cli_check.php');
30
31	$checks = array(
32		'ss_poller',
33		'ss_poller_items',
34		'ss_recache',
35		'ss_boost',
36		'ss_boost_mem',
37		'ss_boost_table',
38		'ss_boost_records',
39		'ss_boost_avg_size',
40		'ss_boost_timing',
41		'ss_export',
42		'ss_thold_time',
43		'ss_thold_checks',
44		'ss_thold_hstats',
45		'ss_syslog_time',
46		'ss_syslog_stats',
47		'ss_monitor_time',
48		'ss_monitor_stats',
49		'ss_report_stats',
50		'ss_spike_time',
51		'ss_spike_stats',
52		'ss_webseer_counts',
53		'ss_webseer_stats',
54	);
55
56	foreach ($checks as $check) {
57		if (function_exists($check)) {
58			print $check . ': ' . call_user_func($check) . PHP_EOL;
59		}
60	}
61}
62
63function ss_thold_time() {
64	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="stats_thold"'));
65
66	$stats = '';
67	foreach ($_stats as $_stat) {
68		if (preg_match('/^Time/', $_stat)) $stats .= str_replace('Time:', '', $_stat);
69	}
70
71	return empty($stats) ? '0' : trim($stats);
72}
73
74function ss_thold_checks() {
75	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="stats_thold"'));
76
77	$stats = '';
78	foreach ($_stats as $_stat) {
79		if (preg_match('/^Tholds/', $_stat)) $stats .= str_replace('Tholds:', '', $_stat);
80	}
81
82	return empty($stats) ? '0' : trim($stats);
83}
84
85function ss_thold_hstats() {
86	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="stats_thold"'));
87
88	$stats = '';
89	foreach ($_stats as $_stat) {
90		if (preg_match('/^TotalDevices/', $_stat)) {
91			$stats .= $_stat . ' ';
92		} elseif (preg_match('/^DownDevices/', $_stat)) {
93			$stats .= $_stat;
94		}
95	}
96
97	return empty($stats) ? 'TotalDevices:0 DownDevices:0' : trim($stats);
98}
99
100function ss_monitor_time() {
101	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="stats_monitor"'));
102
103	$stats = '';
104	foreach ($_stats as $_stat) {
105		if (preg_match('/^Time/', $_stat)) $stats .= str_replace('Time:', '', $_stat);
106	}
107
108	return empty($stats) ? '0' : trim($stats);
109}
110
111function ss_monitor_stats() {
112	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="stats_monitor"'));
113
114	$stats = '';
115	foreach ($_stats as $_stat) {
116		if (preg_match('/^Reboots/', $_stat)) {
117			$stats .= $_stat . ' ';
118		} elseif (preg_match('/^DownDevices/', $_stat)) {
119			$stats .= $_stat . ' ';
120		} elseif (preg_match('/^Notifications/', $_stat)) {
121			$stats .= $_stat . ' ';
122		} elseif (preg_match('/^Purges/', $_stat)) {
123			$stats .= $_stat . ' ';
124		}
125	}
126
127	return empty($stats) ? 'Reboots:0 DownDevices:0 Notifications:0 Purges:0' : trim($stats);
128}
129
130function ss_syslog_time() {
131	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="syslog_stats"'));
132
133	$stats = '';
134	foreach ($_stats as $_stat) {
135		if (preg_match('/^time/', $_stat)) $stats .= str_replace('time:', '', $_stat);
136	}
137
138	return empty($stats) ? '0' : trim($stats);
139}
140
141function ss_syslog_stats() {
142	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="syslog_stats"'));
143
144	$stats = '';
145	foreach ($_stats as $_stat) {
146		if (preg_match('/^deletes/', $_stat)) {
147			$stats .= $_stat . ' ';
148		} elseif (preg_match('/^incoming/', $_stat)) {
149			$stats .= $_stat . ' ';
150		} elseif (preg_match('/^removes/', $_stat)) {
151			$stats .= $_stat . ' ';
152		} elseif (preg_match('/^xfers/', $_stat)) {
153			$stats .= $_stat . ' ';
154		} elseif (preg_match('/^alerts/', $_stat)) {
155			$stats .= $_stat . ' ';
156		} elseif (preg_match('/^alarms/', $_stat)) {
157			$stats .= $_stat . ' ';
158		} elseif (preg_match('/^reports/', $_stat)) {
159			$stats .= $_stat . ' ';
160		}
161	}
162
163	return empty($stats) ? 'deletes:0 incoming:0 removes:0 xfers:0 alerts:0 alarms:0 reports:0' : trim($stats);
164}
165
166function ss_poller() {
167	$stats = db_fetch_cell('SELECT value
168		FROM settings
169		WHERE name="stats_poller"');
170
171	return empty($stats) ? 'Time:0 Method:0 Processes:0 Threads:0 Hosts:0 HostsPerProcess:0 DataSources:0 RRDsProcessed:0' : trim($stats);
172}
173
174function ss_webseer_counts() {
175	$stats = array();
176	if (db_table_exists('plugin_webseer_urls')) {
177		$stats = db_fetch_row('SELECT SUM(triggered) AS triggered,
178			SUM(CASE WHEN triggered=0 THEN 1 ELSE 0 END) AS successful,
179			SUM(CASE WHEN enabled="" THEN 1 ELSE 0 END) AS disabled
180			FROM plugin_webseer_urls');
181	}
182
183
184	return !cacti_sizeof($stats) ? 'triggered:0 successful:0 disabled:0' : 'triggered:' . $stats['triggered'] . ' successful:' . $stats['successful'] . ' disabled:' . $stats['disabled'];
185}
186
187function ss_webseer_stats() {
188	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="stats_webseer"'));
189
190	$stats = '';
191	foreach ($_stats as $_stat) {
192		if (preg_match('/^Time/', $_stat)) {
193			$stats .= $_stat . ' ';
194		} elseif (preg_match('/^Checks/', $_stat)) {
195			$stats .= $_stat . ' ';
196		} elseif (preg_match('/^Servers/', $_stat)) {
197			$stats .= $_stat . ' ';
198		}
199	}
200
201	return empty($stats) ? 'Time:0 Checks:0 Servers:0' : trim($stats);
202}
203
204function ss_poller_items() {
205	$poller_cache = db_fetch_assoc('SELECT action, COUNT(*) AS count
206		FROM poller_item
207		GROUP BY action');
208
209	$entries = array(0, 0, 0);
210
211	if (cacti_sizeof($poller_cache)) {
212		foreach ($poller_cache as $item) {
213			$entries[$item['action']] = $item['count'];
214		}
215	}
216
217	return trim(
218		'snmp:'          . $entries[0] . ' ' .
219		'script:'        . $entries[1] . ' ' .
220		'script_server:' . $entries[2]
221	);
222}
223
224function ss_recache() {
225	$stats = db_fetch_cell('SELECT value
226		FROM settings
227		WHERE name LIKE "stats_recache%"
228		LIMIT 1');
229
230	return empty($stats) ? 'RecacheTime:0 DevicesRecached:0' : trim($stats);
231}
232
233function ss_boost() {
234	$stats = db_fetch_cell('SELECT value
235		FROM settings
236		WHERE name = "stats_boost"');
237
238	return empty($stats) ? 'Time:0 RRDUpdates:0' : trim($stats);
239}
240
241function ss_boost_mem() {
242	$stats = db_fetch_cell('SELECT value
243		FROM settings
244		WHERE name="boost_peak_memory"');
245
246	return empty($stats) ? '0' : trim($stats);
247}
248
249function ss_boost_table() {
250	$stats = db_fetch_cell('SELECT DATA_LENGTH+INDEX_LENGTH AS tbl_len
251		FROM INFORMATION_SCHEMA.TABLES
252		WHERE TABLE_NAME = "poller_output_boost"
253		AND TABLE_SCHEMA = SCHEMA()');
254
255	return empty($stats) ? '0' : trim($stats);
256}
257
258function ss_boost_records() {
259	$stats = db_fetch_cell('SELECT TABLE_ROWS
260		FROM INFORMATION_SCHEMA.TABLES
261		WHERE TABLE_NAME = "poller_output_boost"
262		AND TABLE_SCHEMA = SCHEMA()');
263
264	return empty($stats) ? '0' : trim($stats);
265}
266
267function ss_boost_avg_size() {
268	$stats = db_fetch_cell('SELECT AVG_ROW_LENGTH
269		FROM INFORMATION_SCHEMA.TABLES
270		WHERE TABLE_NAME = "poller_output_boost"
271		AND TABLE_SCHEMA = SCHEMA()');
272
273	return empty($stats) ? '0' : trim($stats);
274}
275
276function ss_boost_timing() {
277	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="stats_detail_boost"'));
278
279	$stats = '';
280	foreach ($_stats as $_stat) {
281		if (preg_match('/^get_records:/', $_stat)) {
282			$stats .= str_replace('get_records', 'rrd_get_records', $_stat) . ' ';
283		} elseif (preg_match('/^results_cycle:/', $_stat)) {
284			$stats .= str_replace('results_cycle', 'rrd_results_cycle', $_stat) . ' ';
285		} elseif (preg_match('/^rrd_filename_and_template:/', $_stat)) {
286			$stats .= str_replace('rrd_filename_and_template', 'rrd_template', $_stat) . ' ';
287		} elseif (preg_match('/^rrd_lastupdate:/', $_stat)) {
288			$stats .= $_stat . ' ';
289		} elseif (preg_match('/^rrdupdate:/', $_stat)) {
290			$stats .= str_replace('rrdupdate', 'rrd_update', $_stat) . ' ';
291		} elseif (preg_match('/^delete:/', $_stat)) {
292			$stats .= str_replace('delete', 'rrd_delete', $_stat) . ' ';
293		}
294	}
295
296	return empty($stats) ? 'get_records:0 results_cycle:0 rrd_filename_and_template:0 rrd_lastupdate:0 rrdupdate:0 delete:0' : trim($stats);
297}
298
299function ss_export() {
300	$_stats = explode(' ', db_fetch_cell('SELECT value FROM settings WHERE name="stats_export"'));
301
302	$stats = '';
303	foreach ($_stats as $_stat) {
304		if (preg_match('/^ExportDuration/', $_stat)) {
305			$stats .= $_stat . ' ';
306		} elseif (preg_match('/^TotalGraphsExported/', $_stat)) {
307			$stats .= $_stat . ' ';
308		}
309	}
310
311	return empty($stats) ? 'ExportDuration:0 TotalGraphsExported:0' : trim($stats);
312}
313
314