forked from Cacti/plugin_thold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_thresholds.php
182 lines (160 loc) · 5.57 KB
/
cli_thresholds.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
<?php
/*
ex: set tabstop=4 shiftwidth=4 autoindent:
+-------------------------------------------------------------------------+
| Copyright (C) 2006-2017 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/ |
+-------------------------------------------------------------------------+
*/
/* do NOT run this script through a web browser */
if (!isset($_SERVER['argv'][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
die('<br><strong>This script is only meant to run at the command line.</strong>');
}
/* We are not talking to the browser */
$no_http_headers = true;
/* let PHP run just as long as it has to */
ini_set('max_execution_time', '0');
error_reporting('E_ALL');
$dir = dirname(__FILE__);
chdir($dir);
if (strpos($dir, 'plugins') !== false) {
chdir('../../');
}
include('./include/global.php');
include_once($config['base_path'] . '/plugins/thold/thold_functions.php');
/* set the defaults */
$force = false;
$debug = false;
$gtemplate = 0;
$ttemplate = 0;
$gids = '';
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
if (sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-auto':
thold_cli_autocreate_host ($value);
exit;
case '--debug':
case '-d':
$debug = true;
break;
case '--force':
case '-f':
$force = true;
break;
case '--graph-template':
case '-gt':
$gtemplate = $value;
break;
case '--thold-template':
case '-tt':
$ttemplate = $value;
break;
case '--graph-ids':
case '-ids':
$gids = $value;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
exit(-1);
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
exit(-1);
}
}
}
/* perform some checks */
if ($ttemplate == 0 && $gtemplate == 0) {
print "ERROR: You must choose either --auto-create or a combination of Graph and Thold Templates\n\n";
display_help();
exit(-1);
}
/* validate values */
if (!is_numeric($ttemplate)) {
print "ERROR: The Thold Template must be numeric.\n\n";
display_help();
exit(-1);
}
/* validate values */
if (!is_numeric($gtemplate)) {
print "ERROR: The Graph Template must be numeric.\n\n";
display_help();
exit(-1);
}
/* validate graph ids */
if (strlen($gids)) {
$gids = explode(" ", $gids);
foreach($gids as $id) {
if (!is_numeric($id)) {
print "ERROR: The Graph ID '$id' is NOT numeric. All Graph ID's must be numeric\n";
display_help();
exit(-1);
}
}
}
function thold_cli_autocreate_host ($id) {
$id = intval($id);
if ($id < 1)
return;
echo "Auto Creating Thresholds for Device #$id\n";
autocreate($id);
if (isset($_SESSION['thold_message'])) {
echo strip_tags(str_replace(array('<br>', 'Created Threshold'), array("\n", ' Created Threshold'), $_SESSION['thold_message']));
}
}
function display_version() {
global $config;
if (!function_exists('plugin_thold_version')) {
include_once($config['base_path'] . '/plugins/thold/setup.php');
}
$info = plugin_thold_version();
echo "Threshold Command Line Interface, Version " . $info['version'] . ", " . COPYRIGHT_YEARS . "\n";
}
/* display_help - displays the usage of the function */
function display_help () {
display_version();
print "\nusage: cli_thresholds.php --auto-create=N | --graph-template=N [--thold-template=N] [--graph-ids='N1 N2 ...']\n\n";
print "There are two usage methods:\n\n";
print "The first requires you to specify the host id of the device and all existing Threshold templates\n";
print "are applied to hosts.\n\n";
print "The second requires you to specify a minimum of a Graph Template ID, and optionally the\n";
print "Threshold Template and Graph ID's of the Graphs to be impacted.\n\n";
print "--auto-create=N - Auto Create all Thresholds for this host id using current templates\n";
print "--graph-template=N - The Graph Template to create Thresholds for\n";
print "--thold-template=N - The Threshold Template to use for creating Thresholds\n";
print "--graph-ids='N1 N2 ...' - The Threshold Template to use for creating Thresholds\n\n";
}