forked from Cacti/plugin_servcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
264 lines (202 loc) · 7.29 KB
/
functions.php
File metadata and controls
264 lines (202 loc) · 7.29 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
<?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 servcheck_show_tab($current_tab) {
global $config, $servcheck_tabs;
if (get_request_var('action') == 'history') {
if ($current_tab == 'servcheck_test.php') {
$current_tab = 'servcheck_test.php?action=history&id=' . get_filter_request_var('id');
$servcheck_tabs[$current_tab] = __('Log History', 'servcheck');
}
}
if (get_request_var('action') == 'graph') {
if ($current_tab == 'servcheck_test.php') {
$current_tab = 'servcheck_web.php?action=graph&id=' . get_filter_request_var('id');
$servcheck_tabs[$current_tab] = __('Graphs', 'servcheck');
}
}
print "<div class='tabs'><nav><ul>";
if (cacti_sizeof($servcheck_tabs)) {
foreach ($servcheck_tabs as $url => $name) {
print "<li><a class='" . (($url == $current_tab) ? 'pic selected' : 'pic') . "' href='" . $config['url_path'] .
"plugins/servcheck/$url'>$name</a></li>";
}
}
print '</ul></nav></div>';
}
function servcheck_check_debug() {
global $debug;
if (!$debug) {
$plugin_debug = read_config_option('selective_plugin_debug');
if (preg_match('/(^|[, ]+)(servcheck)($|[, ]+)/', $plugin_debug, $matches)) {
$debug = (cacti_sizeof($matches) == 4 && $matches[2] == 'servcheck');
}
}
}
function servcheck_debug($message='') {
global $debug;
if ($debug) {
cacti_log('DEBUG: ' . trim($message), true, 'SERVCHECK');
}
}
function servcheck_graph ($id, $interval) {
global $config, $graph_interval;
$result = db_fetch_assoc_prepared("SELECT
lastcheck, duration FROM plugin_servcheck_log
WHERE test_id = ? AND
lastcheck > DATE_SUB(NOW(), INTERVAL ? HOUR)
ORDER BY id", array($id, $interval));
if (cacti_sizeof($result) < 5) {
print __('No data', 'servcheck');
return;
}
$xid = 'xx' . substr(md5($graph_interval[$interval]), 0, 7);
foreach ($result as $row) {
$lastcheck[] = $row['lastcheck'];
$duration[] = round($row['duration'], 5);
}
// Start chart attributes
$chart = array(
'bindto' => "#line_$xid",
'size' => array(
'height' => 300,
'width'=> 600
),
'point' => array (
'r' => 1.5
),
'data' => array(
'type' => 'area',
'x' => 'x',
'xFormat' => '%Y-%m-%d %H:%M:%S' // rikam mu, jaky je format te timeserie
)
);
$columns = array();
$axis = array();
$axes = array();
// Add the X Axis first
$columns[] = array_merge(array('x'), $lastcheck);
$columns[] = array_merge(array('Duration'), $duration);
// $columns[] = array_merge(array('Connect'), $connect_time);
// $columns[] = array_merge(array('DNS '), $namelookup_time);
// Setup the Axis
$axis['x'] = array(
'type' => 'timeseries',
'tick' => array(
'format'=> '%m-%d %H:%M',
'culling' => array('max' => 6),
)
);
$axis['y'] = array(
'tick' => array(
'label' => array(
'text' => 'Response in ms',
),
'show' => true
)
);
$chart['data']['axes']= $axes;
$chart['axis']= $axis;
$chart['data']['columns'] = $columns;
$chart_data = json_encode($chart);
$content = '<div id="line_' . $xid. '"></div>';
$content .= '<script type="text/javascript">';
$content .= 'line_' . $xid . ' = bb.generate(' . $chart_data . ');';
$content .= '</script>';
print $content;
}
// It is not secure, it is better than plaintext
// will be removed in 0.5
function servcheck_hide_text ($string) {
$output = '';
for ($f = 0; $f < strlen($string); $f++) {
$output .= dechex(ord($string[$f]));
}
return $output;
}
function servcheck_show_text ($string) {
$output = '';
for ($f = 0; $f < strlen($string); $f = $f + 2) {
$output .= chr(hexdec($string[$f] . $string[($f+1)]));
}
return $output;
}
function servcheck_encrypt_credential ($cred) {
$servcheck_key = read_user_setting('servcheck_key', null, true, 1);
$iv_length = intval(openssl_cipher_iv_length(SERVCHECK_CIPHER));
$servcheck_iv = openssl_random_pseudo_bytes($iv_length);
if (is_null($servcheck_key)) {
cacti_log('Creating new cipher key', 'servcheck');
$servcheck_key = hash('sha256', 'ksIBWE' . date('hisv'));
set_user_setting('servcheck_key', base64_encode($servcheck_key));
} else {
$servcheck_key = base64_decode($servcheck_key);
}
$encrypted = openssl_encrypt(json_encode($cred), SERVCHECK_CIPHER, $servcheck_key, OPENSSL_RAW_DATA, $servcheck_iv);
return base64_encode($servcheck_iv . $encrypted);
}
function servcheck_decrypt_credential ($cred_id) {
$servcheck_key = read_user_setting('servcheck_key', null, true, 1);
if (is_null($servcheck_key)) {
cacti_log('Cannot decrypt credential, key is missing', 'servcheck');
return false;
} else {
$servcheck_key = base64_decode($servcheck_key);
}
$encrypted = db_fetch_cell_prepared('SELECT data FROM plugin_servcheck_credential
WHERE id = ?',
array($cred_id));
$encrypted = base64_decode($encrypted);
$iv_length = intval(openssl_cipher_iv_length(SERVCHECK_CIPHER));
$servcheck_iv = substr($encrypted, 0, $iv_length);
$encrypted = substr($encrypted, $iv_length);
$decrypted = openssl_decrypt ($encrypted, SERVCHECK_CIPHER, $servcheck_key, OPENSSL_RAW_DATA, $servcheck_iv);
return json_decode($decrypted, true);
}
function servcheck_legend() {
global $servcheck_states;
html_start_box('', '100%', false, '3', 'center', '');
print '<tr class="tableRow">';
foreach ($servcheck_states as $index => $state) {
print '<td style="background-color: ' . $state['color'] . '">' . $state['display'] . '</td>';
}
print '</tr>';
html_end_box(false);
}
// check if summer/winter time changed in last few hours. We need to know it because of expired/renew certificates
function servcheck_summer_time_changed() {
$hours = 8;
if (date_default_timezone_get() === '') {
return false;
}
$now = new DateTime('now');
$past = (clone $now)->modify("-{$hours} hours");
$offsetNow = $now->getOffset();
$offsetPast = $past->getOffset();
$diff = $offsetNow - $offsetPast;
if ($diff !== 0) {
return true;
} else {
return false;
}
}