-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutdbfunctions.php
More file actions
348 lines (332 loc) · 13.8 KB
/
utdbfunctions.php
File metadata and controls
348 lines (332 loc) · 13.8 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/*---------------------------------------------------------------------------+
| Copyright (C) 2013 Eric Stewart |
| |
| 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. |
+---------------------------------------------------------------------------+
| Unified Trees: Cacti Plugin to unify trees from multiple Cacti servers |
+---------------------------------------------------------------------------+
| Code designed, written, maintained (as of 2013/2014) by Eric Stewart |
+---------------------------------------------------------------------------+
*/
/* 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>");
}
*/
/* 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_once("./include/global.php");
include_once($config["library_path"] . "/database.php");
include_once($config["base_path"] . '/lib/tree.php');
// Set up the tree table.
function ut_setup_tree_table() {
$data = array();
$data['columns'][] = array('name' => 'id', 'type' => 'int(8)', 'NULL' => false, 'auto_increment' => true);
$data['columns'][] = array('name' => 'tier', 'type' => 'int(4)', 'NULL' => false, 'default' => 0);
$data['columns'][] = array('name' => 'treename', 'type' => 'varchar(30)', 'NULL' => false, 'default' => '');
$data['columns'][] = array('name' => 'name', 'type' => 'varchar(30)', 'NULL' => false, 'default' => '');
$data['columns'][] = array('name' => 'fullname', 'type' => 'varchar(255)', 'NULL' => false, 'default' => '');
// $data['columns'][] = array('name' => 'xID', 'type' => 'varchar(20)', 'NULL' => false, 'default' => '');
$data['columns'][] = array('name' => 'url', 'type' => 'varchar(150)', 'NULL' => false, 'default' => '');
$data['columns'][] = array('name' => 'host_id', 'type' => 'int(8)', 'NULL' => false, 'default' => 0);
$data['primary'] = 'id';
$data['keys'][] = array();
$data['type'] = 'memory';
$data['comment'] = 'Plugin Unified Trees - Holds the built tree.';
api_plugin_db_table_create('unifiedtrees', 'plugin_unifiedtrees_tree', $data);
}
// Save the tree to the table
function ut_save_tree($fulltree) {
foreach($fulltree['tree'] as $treename => $tree) {
foreach($tree['id'] as $leafid => $leaf) {
$tier = $leaf['tier'];
$name = $leaf['name'];
$fullname = $leaf['fullname'];
// $xID = $leaf['xID'];
$url = $leaf['url'];
$host_id = $leaf['host_id'];
db_execute("INSERT INTO plugin_unifiedtrees_tree (tier, treename, name, fullname, url, host_id) VALUES ($tier, '$treename', '$name', '$fullname', '$url', $host_id)");
}
}
}
// Read the tree in from a table and optionally specify the server to get it from
function ut_read_tree($database = FALSE) {
global $cnn_id;
if (!$database) {
$database = $cnn_id;
}
$fulltree = array();
$leaves = db_fetch_assoc("SELECT * from plugin_unifiedtrees_tree ORDER BY id", TRUE, $database);
foreach ($leaves as $leaf) {
$fulltree['tree'][$leaf['treename']]['id'][$leaf['fullname']]['fullname'] = $leaf['fullname'];
$fulltree['tree'][$leaf['treename']]['id'][$leaf['fullname']]['tier'] = $leaf['tier'];
$fulltree['tree'][$leaf['treename']]['id'][$leaf['fullname']]['name'] = $leaf['name'];
$fulltree['tree'][$leaf['treename']]['id'][$leaf['fullname']]['xID'] = $leaf['xID'];
$fulltree['tree'][$leaf['treename']]['id'][$leaf['fullname']]['url'] = $leaf['url'];
$fulltree['tree'][$leaf['treename']]['id'][$leaf['fullname']]['host_id'] = $leaf['host_id'];
}
return $fulltree;
}
// Build a tree from multiple databases.
function ut_build_tree($databases) {
$btid = 1;
$treenames = array();
foreach($databases as $db) {
utdb_debug($db['base_url']."\n");
$tiername = array();
$trees = db_fetch_assoc("SELECT * from graph_tree", TRUE, $db['dbconn']);
utdb_debug("Tree size ".sizeof($trees)."\n");
// IDs in xID must be unique within the built tree. But the URL should refer
// to the tree ID on the server in question.
// On second thought, let's remove the xID stuff from here and put it in
// print_tree.
foreach($trees as $tree) {
// if(!isset($treenames[$tree['name']])) {
// $treenames[$tree['name']]['tid'] = $btid;
// $treenames[$tree['name']]['lid'] = 1;
// ++$btid;
// }
$tiername[0] = "0";
if(!isset($fulltree['tree'][$tree['name']]['id'][$tiername[0]])) {
$fulltree['tree'][$tree['name']]['id'][$tiername[0]]['tier'] = 0;
$fulltree['tree'][$tree['name']]['id'][$tiername[0]]['fullname'] = $tiername[0];
$fulltree['tree'][$tree['name']]['id'][$tiername[0]]['name'] = $tree['name'];
// $fulltree['tree'][$tree['name']]['id'][$tiername[0]]['xID'] = "tree_".$tree['id'];
//$fulltree['tree'][$tree['name']]['id'][$tiername[0]]['xID'] = $treenames[$tree['name']]['tid'];
$fulltree['tree'][$tree['name']]['id'][$tiername[0]]['url'] = $db['base_url']."graph_view.php?action=tree&tree_id=".$tree['id'];
$fulltree['tree'][$tree['name']]['id'][$tiername[0]]['host_id'] = 0;
}
$currenttier = 0;
$treeitems = db_fetch_assoc("SELECT graph_tree_items.*, host.description, host.disabled
FROM graph_tree_items
LEFT JOIN host
ON (graph_tree_items.host_id=host.id)
WHERE graph_tree_items.graph_tree_id=".$tree['id']."
ORDER BY graph_tree_items.order_key", TRUE, $db['dbconn']);
utdb_debug("Treeitems: ".sizeof($treeitems)."\n");
if (is_array($treeitems)) {
foreach($treeitems as &$treeitem) {
$tier = tree_tier($treeitem['order_key']);
$url = $db['base_url']."graph_view.php?action=tree&tree_id=".$tree['id']."&leaf_id=".$treeitem['id'];
if(isset($treeitem['title']) && $treeitem['title'] != "" && $treeitem['host_id'] == 0) {
$stn = $treeitem['title']."|".$tier;
}elseif(isset($treeitem['description']) && $treeitem['host_id'] != 0) {
if($treeitem['disabled'] == "on") {
$treeitem['description'] = $treeitem['description']." (D)";
}
$stn = $treeitem['description']."|".$tier;
}else{
utdb_debug("No host_id/title\n");
}
if($tier == $currenttier) {
$tiername[$tier] = $tiername[$tier-1]."|".$stn;
}elseif ($tier > $currenttier) {
$tiername[$tier] = $tiername[$currenttier]."|".$stn;
$currenttier = $tier;
}elseif ($tier < $currenttier) {
$tiername[$tier] = $tiername[$tier-1]."|".$stn;
$currenttier = $tier;
}
if(!isset($fulltree['tree'][$tree['name']]['id'][$tiername[$tier]])) {
if($treeitem['host_id'] > 0) {
$fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['name'] = $treeitem['description'];
}else{
$fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['name'] = $treeitem['title'];
}
$fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['tier']=$tier;
$fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['host_id']=$treeitem['host_id'];
$fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['fullname']=$tiername[$tier];
// $fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['xID']="tree_".$treenames[$tree['name']]."_leaf_".$treeitem['id'];
//$fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['xID']=$treenames[$tree['name']]['tid']."_".$treenames[$tree['name']]['lid'];
$fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['url']=$url;
// $fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['url']=$url."&xID=".$fulltree['tree'][$tree['name']]['id'][$tiername[$tier]]['xID'];
}
++$treenames[$tree['name']]['lid'];
}
}else{
print "Not an array $treeitems\n";
}
}
}
return $fulltree;
}
// Servers and Always configurations need this.
function ut_setup_dbs() {
global $cnn_id, $config, $database_default, $database_username, $database_port, $database_password, $url_path;
$answer = array();
// Configure the local DB:
$ut_base_url = read_config_option("unifiedtrees_base_url");
if(isset($ut_base_url) && $ut_base_url != "") {
$answer[0]['base_url'] = $ut_base_url;
}else{
$answer[0]['base_url'] = $url_path;
}
$answer[0]['dbconn'] = $cnn_id;
// ORDER BY so that *maybe* two servers will have trees that resemble one
// one another. You can always hope.
$remote = db_fetch_assoc("SELECT * FROM plugin_unifiedtrees_sources
WHERE enable_db='on' ORDER BY db_address");
foreach ($remote as $other) {
$tmp = array();
if(!isset($other['db_name']) || $other['db_name'] == '') {
$other['db_name'] = $database_default;
}
if(!isset($other['db_uname']) || $other['db_uname'] == '') {
$other['db_uname'] = $database_username;
}
if(!isset($other['db_pword']) || $other['db_pword'] == '') {
$other['db_pword'] = $database_password;
}
if(!isset($other['db_port']) || $other['db_port'] == 0 || $other['db_port'] == '') {
if(!isset($database_port) || $database_port == '') {
$other['db_port'] = "3306";
}else{
$other['db_port'] = $database_port;
}
}
if(!isset($other['db_retries']) || $other['db_retries'] == 0 ||
$other['db_retries'] == '') {
$other['db_retries'] = 2;
}
if(!isset($other['base_url']) || $other['base_url'] == '') {
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$other['base_url'] = "https://";
}else{
$other['base_url'] = "http://";
}
$other['base_url'] .= $other['db_address'].$url_path;
}
$dsn = $other['db_type']."://".rawurlencode($other['db_uname']).":".rawurlencode($other['db_pword'])."@".rawurlencode($other['db_address'])."/".rawurlencode($other['db_name'])."?persist";
if($other['db_ssl'] == 'on') {
if($other['db_type'] == "mysql") {
$dsn .= "&clientflags=" . MYSQL_CLIENT_SSL;
}elseif ($other['db_type'] == "mysqli") {
$dsn .= "&clientflags=" . MYSQLI_CLIENT_SSL;
}
}
if($other['db_port'] != "3306") {
$dsn .= "&port=" . $port;
}
$attempt = 0;
while($attempt < $other['db_retries']) {
$oconn = ADONewConnection($dsn);
if($oconn) {
$tmp['base_url'] = $other['base_url'];
$tmp['dbconn'] = $oconn;
$answer[] = $tmp;
break;
}
$attempt++;
}
$disable_bad = read_config_option('unifiedtrees_disable_bad_connection');
$admin_email = read_config_option('unifiedtrees_admin_email');
if(!$oconn && $disable_bad == 'on' &&
filter_var($admin_email, FILTER_VALIDATE_EMAIL)) {
db_execute("UPDATE plugin_unifiedtrees_sources SET enable_db=''
WHERE id=".$other['id']);
$message = "Disabled connection for unified trees:
SERVER: ".$other['db_address']."
DB NAME: ".$other['db_name']."
DB USER: ".$other['db_uname']."
BASEURL: ".$other['base_url']."
";
mail($admin_email, "UT DISABLED: ".$other['db_address'], $message);
}
}
return $answer;
}
// For clients, we want the first server that qualifies.
function ut_get_server_db() {
global $config, $database_default, $database_username, $database_port, $database_password;
$remote = db_fetch_assoc("SELECT * FROM plugin_unifiedtrees_sources
WHERE enable_db='on'");
foreach ($remote as $other) {
unset($why);
$tmp = array();
if(!isset($other['db_name']) || $other['db_name'] == '') {
$other['db_name'] = $database_default;
}
if(!isset($other['db_uname']) || $other['db_uname'] == '') {
$other['db_uname'] = $database_username;
}
if(!isset($other['db_pword']) || $other['db_pword'] == '') {
$other['db_pword'] = $database_password;
}
if(!isset($other['db_port']) || $other['db_port'] == 0 || $other['db_port'] == '') {
if(!isset($database_port) || $database_port == '') {
$other['db_port'] = "3306";
}else{
$other['db_port'] = $database_port;
}
}
if(!isset($other['db_retries']) || $other['db_retries'] == 0 ||
$other['db_retries'] == '') {
$other['db_retries'] = 2;
}
$dsn = $other['db_type']."://".rawurlencode($other['db_uname']).":".rawurlencode($other['db_pword'])."@".rawurlencode($other['db_address'])."/".rawurlencode($other['db_name'])."?persist";
if($other['db_ssl'] == 'on') {
if($other['db_type'] == "mysql") {
$dsn .= "&clientflags=" . MYSQL_CLIENT_SSL;
}elseif ($other['db_type'] == "mysqli") {
$dsn .= "&clientflags=" . MYSQLI_CLIENT_SSL;
}
}
if($other['db_port'] != "3306") {
$dsn .= "&port=" . $port;
}
$attempt = 0;
while($attempt < $other['db_retries']) {
$oconn = ADONewConnection($dsn);
// Just retrun the first one that's live.
if($oconn) {
$tables = db_fetch_assoc("SHOW TABLES LIKE 'plugin_unifiedtrees_tree'", TRUE, $oconn);
if(sizeof($tables > 0)) {
return($oconn);
break;
}else{
$why = "Could not find table plugin_unifiedtrees_tree\n";
}
}
$attempt++;
}
$disable_bad = read_config_option('unifiedtrees_disable_bad_connection');
$admin_email = read_config_option('unifiedtrees_admin_email');
if($disable_bad == 'on' &&
filter_var($admin_email, FILTER_VALIDATE_EMAIL)) {
db_execute("UPDATE plugin_unifiedtrees_sources SET enable_db=''
WHERE id=".$other['id']);
if(!isset($why)) {
$why = "Could not connect to database\n";
}
$message = "Disabled connection for unified trees:
SERVER: ".$other['db_address']."
DB NAME: ".$other['db_name']."
DB USER: ".$other['db_uname']."
BASEURL: ".$other['base_url']."
$why";
mail($admin_email, "UT DISABLED: ".$other['db_address'], $message);
}
}
return FALSE;
}
function utdb_debug($msg) {
global $debug;
if($debug) print $msg;
}
?>