forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repair_graphs.php
executable file
·224 lines (178 loc) · 7.48 KB
/
repair_graphs.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
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
#!/usr/bin/env php
<?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/ |
+-------------------------------------------------------------------------+
*/
/* Original script is located here https://forums.cacti.net/viewtopic.php?t=35816, but this one was modified quite a lot */
require(__DIR__ . '/../include/cli_check.php');
require_once(CACTI_PATH_LIBRARY . '/poller.php');
require_once(CACTI_PATH_LIBRARY . '/utility.php');
require_once(CACTI_PATH_LIBRARY . '/template.php');
/* switch to main database for cli's */
if ($config['poller_id'] > 1) {
db_switch_remote_to_main();
}
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
$execute = false;
$show_sql = false;
unset($host_id);
unset($graph_template_id);
unset($data_template_id);
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter, 2);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--execute':
$execute = true;
break;
case '--show-sql':
$show_sql = true;
break;
case '--host-id':
$host_id = trim($value);
if (!is_numeric($host_id)) {
print "ERROR: You must supply a valid host-id to run this script!\n";
exit(1);
}
break;
case '--graph-template-id':
$graph_template_id = $value;
if (!is_numeric($graph_template_id)) {
print "ERROR: You must supply a numeric graph-template-id!\n";
exit(1);
}
break;
case '--data-template-id':
$data_template_id = $value;
if (!is_numeric($data_template_id)) {
print "ERROR: You must supply a numeric data-template-id!\n";
exit(1);
}
break;
case '--version':
case '-v':
case '-V':
display_version();
exit(0);
case '--help':
case '-h':
case '-H':
display_help();
exit(0);
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
exit(1);
}
}
if (!$show_sql && !$execute) {
display_help();
exit(1);
}
if (!isset($data_template_id)) {
print "ERROR: You must supply a valid data-template-id!\n";
exit(1);
}
if (!isset($graph_template_id)) {
print "ERROR: You must supply a valid graph-template-id!\n";
exit(1);
}
if ($execute) {
print "NOTE: Repairing Graphs\n";
} else {
print "NOTE: Performing Check of Graphs\n";
}
// Get all graphs for supplied graph template
$graph = db_fetch_assoc('SELECT *
FROM graph_local
WHERE ' . (!isset($host_id) ? '' : 'host_id='.$host_id.' AND ') . ' graph_template_id=' . $graph_template_id . '');
if (cacti_sizeof($graph)) {
if (!$show_sql) {
print "\nCorrupted graphs:\n";
}
foreach ($graph as $g) {
// Get datasource for supplied data template for current host
$ds = db_fetch_assoc('SELECT * FROM data_local where host_id=' . $g['host_id'] . ' and data_template_id=' . $data_template_id);
if (!cacti_sizeof($ds)) {
continue;
}
$ds = $ds[0];
// Get rrd for found datasource
$rrd_data = db_fetch_assoc('SELECT * FROM data_template_rrd where local_data_id=' . $ds['id']);
if (!cacti_sizeof($rrd_data)) {
print 'Could not get correct rrd id for datasource=' . $ds['id'] . "\n";
continue;
}
/*
// Here we will find graph items that should point to our data template
// Get templated rrd id for given data template
select id from data_template_rrd where local_data_template_rrd_id=0 and local_data_id=0 and data_template_id=520
// Get templated graph->rrd association
select id from graph_templates_item where local_graph_template_item_id=0 and local_graph_id=0 and task_item_id=
// Get graph associations which corresponds to supplied data template
select id from graph_templates_item where local_graph_id=$g["id"] and local_graph_template_item_id in
// But I'm too lazy to write such a lot of code, so let's better make one long query below
*/
$graph_templates_items_wrong = db_fetch_assoc('select id, task_item_id from graph_templates_item WHERE task_item_id!=' . $rrd_data[0]['id'] . ' and graph_template_id=' . $graph_template_id . ' and local_graph_id=' . $g['id'] . ' and local_graph_template_item_id in (select id from graph_templates_item where local_graph_template_item_id=0 and local_graph_id=0 and task_item_id=(select id from data_template_rrd where local_data_template_rrd_id=0 and local_data_id=0 and data_template_id=' . $data_template_id . '))');
if (!cacti_sizeof($graph_templates_items_wrong)) {
// Everything correct here.
continue;
} else {
foreach ($graph_templates_items_wrong as $graph_templates_item_wrong) {
// Here is a list of graph_templates_item ids to be fixed and their wrong task_item_id
$graph_templates_item[] = $graph_templates_item_wrong['id'];
$task_item_id[] = $graph_templates_item_wrong['task_item_id'];
}
}
print 'Host ' . $g['host_id'] . ', graph ' . $g['id'] . ', graph item ' . implode(',',$graph_templates_item) . ', task_item_id ' . implode(',',$task_item_id) . '->' . $rrd_data[0]['id'] . "\n";
$query = 'UPDATE graph_templates_item SET task_item_id=' . $rrd_data[0]['id'] . ' WHERE task_item_id!=' . $rrd_data[0]['id'] . ' and graph_template_id=' . $graph_template_id . ' and local_graph_id=' . $g['id'] . ' and id in (' . implode(',',$graph_templates_item) . ')';
if ($show_sql) {
print $query . ";\n";
}
if ($execute) {
db_execute($query);
}
unset($graph_templates_item);
unset($task_item_id);
}
}
function display_version() {
$version = get_cacti_cli_version();
print "Cacti Graph Repair Tool, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/* display_help - displays the usage of the function */
function display_help() {
print "usage: repair_graphs.php [--host-id=ID] --data-template-id=[ID]\n";
print " --graph-template-id=[ID] [--show-sql] [--execute]\n\n";
print "Cacti utility for repairing graph<->datasource relationship via a command line interface.\n\n";
print "--execute - Perform the repair\n";
print "--show-sql - Show SQL lines for the repair (optional)\n";
print "--host-id=id - The host_id to repair or leave empty to process all hosts\n";
print "--data-template-id=id - The numerical ID of the data template to be fixed\n";
print "--graph-template-id=id - The numerical ID of the graph template to be fixed\n";
}