forked from Cacti/plugin_thold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_import.php
175 lines (141 loc) · 4.91 KB
/
cli_import.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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 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/ |
+-------------------------------------------------------------------------+
*/
/* let PHP run just as long as it has to */
ini_set('max_execution_time', '0');
error_reporting(E_ALL);
include(dirname(__FILE__) . '/../../include/cli_check.php');
include_once($config['base_path'] . '/lib/xml.php');
include_once($config['base_path'] . '/plugins/thold/thold_functions.php');
/* set the defaults */
$force = false;
$debug = false;
$filename = false;
$errors = 0;
/* 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 '--debug':
case '-d':
$debug = true;
break;
case '--filename':
case '-f':
$filename = $value;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
}
}
/* validate filename set */
if ($filename === false) {
print 'ERROR: You must specify a filename using either the --filename of -f options.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
/* validate exists */
if (!file_exists($filename)) {
print 'ERROR: The filename \'' . $filename . '\' does not exist.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
/* validate readable */
if (!is_readable($filename)) {
print 'ERROR: The filename \'' . $filename . '\' is not readable.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
/* validate good xml */
$data = file_get_contents($filename);
$xml = xml2array($data);
if (!is_array($xml) || !cacti_sizeof($xml)) {
print 'ERROR: The filename \'' . $filename . '\' is not a valid thold XML file.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
$return_data = thold_template_import($data);
if (sizeof($return_data) && isset($return_data['success'])) {
foreach($return_data['success'] as $message) {
print 'NOTE: ' . $message . PHP_EOL;
}
}
if (isset($return_data['errors'])) {
foreach($return_data['errors'] as $error) {
print 'ERROR: ' . $error . PHP_EOL;
}
}
if (isset($return_data['failure'])) {
foreach($return_data['failure'] as $error) {
print 'ERROR: ' . $error . PHP_EOL;
}
$errors++;
}
if ($errors == 0) {
$message = 'NOTE: Import of Threshold Template file \'' . $filename . '\' succeeded.';
cacti_log($message, false, 'THOLD');
print $message . PHP_EOL;
exit(0);
} else {
$message = 'ERROR: Import of Threshold Template file \'' . $filename . '\' failed. There were \'' . $errors . '\' encountered.';
cacti_log($message, false, 'THOLD');
print $message . PHP_EOL;
exit(1);
}
function display_version() {
global $config;
if (!function_exists('plugin_thold_version')) {
include_once($config['base_path'] . '/plugins/thold/setup.php');
}
$info = plugin_thold_version();
print 'Threshold Template Import Utility, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . PHP_EOL;
}
/* display_help - displays the usage of the function */
function display_help () {
display_version();
print PHP_EOL;
print 'usage: cli_import.php --filename=N' . PHP_EOL . PHP_EOL;
print 'Simple CLI command to import a threshold template.' . PHP_EOL . PHP_EOL;
print '--filename=N - A valid threshold template exported using the thold plugin.' . PHP_EOL . PHP_EOL;
}