-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_xml.php
237 lines (210 loc) · 7.13 KB
/
create_xml.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
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* Creates XML files for report generation.
*
* PHP version 5
*
* @category PHP
* @package Report generation
* @author Rostyslav Fridman <[email protected]>
* @author Nikita Koptel <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link http://netxms.example.com
*/
require_once 'dbconfig.php';
require_once 'baseReport.php';
require_once 'genPrimaryXML.php';
require_once 'createSubReport.php';
$file = 'credentials.json';
if (file_exists($file)) {
$json = json_decode(file_get_contents($file), true);
} else {
die("Cannot read credentials file" . PHP_EOL);
}
$conn_string = "host=$json[host] port=$json[port] dbname=$json[dbname] user=$json[username] password=$json[password] sslmode=$json[sslmode]";
try {
$dbconn = pg_connect($conn_string);
if ( $dbconn ) {
echo "Connection to DB succesfull" . PHP_EOL;
}
} catch ( Exception $e ) {
echo $e->getMessage();
}
$query = prepareQuery();
// Example intervals for reports
$intervals = array( "1 day" => "daily", "7 days" => "weekly", "1 month" => "monthly" );
foreach ( $intervals as $interval => $hrf_interval ) { // HRF = Human Readable Format
$result = pg_query($query);
if ( !$result ) {
echo "An error occurred" . PHP_EOL;
}
$mainReport = new GenPrimaryXML();
$mainReport->setFileName("$hrf_interval.jrxml");
$mainReport->createXML();
echo $mainReport->getFileName("$hrf_interval.jrxml") . PHP_EOL;
$sorted_results = array();
while ($row = pg_fetch_row($result)) {
$sorted_results[$row[1]] = $row[0];
}
natsort($sorted_results);
foreach ($sorted_results as $node_id => $node_name) {
if ( !isset( $node_name ) || !isset( $node_id ) ) {
die( "Query error or out of range" . PHP_EOL );
}
/*
One graph on page example
*/
$subquery = " AND (items.name = 'System.CPU.Usage')";
$description = "system cpu usage";
populateSubReport($mainReport, $node_id, $node_name, $interval, $hrf_interval, $description, $subquery, $subquery2, $first_color, $second_color, $parameter = null, $graph_type);
/*
Two graphs on page example
*/
$subquery = " AND (items.name = 'System.CPU.Usage')";
$subquery2 = " AND (items.name = 'System.Memory.Physical.Free')";
$description = "system load";
$first_color = "#009933";
$second_color = "#FF9933";
$graph_type = "two on page";
populateSubReport($mainReport, $node_id, $node_name, $interval, $hrf_interval, $description, $subquery);
/*
Use of parameter example
We are changing NetXMS local variables to readable names
This can slow down the generation of graphs
*/
$subquery = " AND (items.name = 'System.CPU.Usage'" .
" AND items.name = 'System.Memory.Physical.Free'";
$first_color = "#9999FF";
$second_color = "#FFCC00";
$description = "human readable system load";
$parameter = "CASE
WHEN items.name = 'System.CPU.Usage' THEN 'CPU Load'
WHEN items.name = 'System.Memory.Physical.Free' THEN 'Free memory'
END";
populateSubReport($mainReport, $node_id, $node_name, $interval, $hrf_interval, $description, $subquery, $subquery2 = null, $first_color, $second_color, $parameter);
}
// Add closing tags on the main reports
$mainReport->closeXML();
}
pg_close($dbconn);
/**
* Populates subreport
*
* @param object $report Report object
* @param string $node_id Node ID
* @param string $node_name Node name
* @param string $interval Interval
* @param string $hrf_interval HRF interval
* @param string $description Description
* @param string $subquery SubQuery
* @param string $subquery2 Second subquery
* @param string $first_color First color
* @param string $second_color Second color
* @param string $parameter Parameter
* @param string $graph_type Graph type
*
* @return null
*/
function populateSubReport(
$report,
$node_id,
$node_name,
$interval,
$hrf_interval,
$description,
$subquery,
$subquery2 = null,
$first_color = null,
$second_color = null,
$parameter = "items.name",
$graph_type = "one on page"
) {
$file_name = resolveFileName($node_name, $description, $hrf_interval);
if ( !file_exists($file_name) ) {
$subReport = new CreateSubReport();
$subReport->setNodeId($node_id);
$subReport->setNodeName($node_name);
$subReport->setInterval($interval);
$subReport->setHRFInterval($hrf_interval);
$subReport->setFileName($file_name);
$subReport->setSubQuery($subquery);
$subReport->setDescription($description);
$subReport->setParameter($parameter);
if ( $graph_type == "two on page" ) {
$subReport->setSubQuery2($subquery2);
$subReport->setFirstColor($first_color);
$subReport->setSecondColor($second_color);
$subReport->createTwoGraphXML();
} else {
$subReport->createOneGraphXML();
}
echo $subReport->getFileName() . PHP_EOL;
$report->setSubReportUUID($subReport->getUUID());
$report->setSubReportName($file_name);
$report->updateXML();
} else {
$xml = simplexml_load_file($file_name);
$header = $xml->attributes();
$report->setSubReportUUID($header[ 'uuid' ]);
$report->setSubReportName($file_name);
$report->updateXML();
}
}
/**
* Resolves file name
*
* @param string $node_name Node name
* @param string $description Description
* @param string $hrf_interval HRF interval
*
* @return string
*/
function resolveFileName( $node_name, $description, $hrf_interval )
{
$uname = $node_name;
$udescription = $description;
$uname = str_replace(' ', '_', $uname);
$udescription = str_replace(' ', '_', $udescription);
$file_name = $uname . '-' . $hrf_interval . '-' . $udescription . '.jrxml';
return $file_name;
}
/**
* Reads from file
*
* @param string $filename File name
*
* @return string
*/
function getIncludeContents($filename)
{
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
/**
* Generates UUID
*
* @return null
*/
function generateUuidV4()
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}