-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsunami_viewer.php
244 lines (206 loc) · 7.26 KB
/
tsunami_viewer.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
238
239
240
241
242
243
244
#!/usr/bin/php
<?php
include 'include/tsunami_common.php';
include 'config/tsunami_config.php';
//parse the hosts configuration
include 'include/tsunami_host_parser.php';
//parse the collector output
include 'include/tsunami_collected_parser.php';
echo date("Ymd H:i:s")." Starting viewer build.\n";
////////////////
//viewer functions
function insertTimeframes()
{
global $aGraphTimeframes;
$timeframe = $aGraphTimeframes[0];
echo '<a href="//<?php echo $_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"]; ?>?timeframe='.$timeframe->m_desc.'">'.$timeframe->m_desc.'</a>';
for($timeframeIter = 1; $timeframeIter < count($aGraphTimeframes); $timeframeIter++)
{
$timeframe = $aGraphTimeframes[$timeframeIter];
echo ' | <a href="//<?php echo $_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"]; ?>?timeframe='.$timeframe->m_desc.'">'.$timeframe->m_desc.'</a>';
}
}
function insertHostGraph($host, $graphID, $preview = false)
{
global $wwwHostname;
global $wwwPathGraphs;
foreach($host->m_graphs as $graph)
{
if($graph->m_name == $graphID)
{
$URLGraph = "//".$wwwHostname.$wwwPathGraphs.$host->m_id."/".$host->m_id."-".$graph->m_name."-"."<?php echo \$timeframe; ?>";
if($preview)
$URLGraph .= "-preview.png";
else
$URLGraph .= ".png";
echo '<img src="'.$URLGraph.'">';
}
}
}
function insertSubhostGraph($host, $subhost, $graphID, $preview = false)
{
global $wwwHostname;
global $wwwPathGraphs;
foreach($host->m_graphsWalk as $graph)
{
if($graph->m_name == $graphID)
{
if($preview)
$URLGraph = "//".$wwwHostname.$wwwPathGraphs.$host->m_id."/".$subhost."/".$host->m_id."-".$subhost."-".$graph->m_name."-"."<?php echo \$timeframe; ?>" . "-preview.png";
else // realtime
$URLGraph .= "//".$wwwHostname."/makeGraph.php?id=".$host->m_id."-".$subhost."-".$graph->m_name."-"."<?php echo \$timeframe; ?>";
echo '<img src="'.$URLGraph.'">';
}
}
}
function insertHostQuery($host, $queryID)
{
foreach($host->m_queries as $query)
{
if($query->m_id == $queryID)
{
echo '<?php echo CleanSNMPResult(snmp2_get("'.$host->m_host.'", "'.$host->m_community.'", "'.$query->m_oid.'")); ?>';
break;
}
}
}
function insertSubhostQuery($host, $subhost, $queryID)
{
foreach($host->m_queriesWalk as $query)
{
if($query->m_id == $queryID)
{
if($queryID == "ASSOCIATION")
echo '<?php
$result = ReadableAssociation(CleanSNMPResult(snmp2_get("'.$host->m_host.'", "'.$host->m_community.'", "'.$query->m_oid.".".(int)$subhost.'")));
if($result == "Unassociated")
$result = "<span class=\"span_warning\">".$result."</span>";
echo $result; ?>';
else if($queryID == "DISTANCE")
echo '<?php echo ReadableDistance(CleanSNMPResult(snmp2_get("'.$host->m_host.'", "'.$host->m_community.'", "'.$query->m_oid.".".(int)$subhost.'"))); ?>';
else if(preg_match("/MIR/", $queryID) or preg_match("/CIR/", $queryID))
echo '<?php
$result = CleanSNMPResult(snmp2_get("'.$host->m_host.'", "'.$host->m_community.'", "'.$query->m_oid.".".(int)$subhost.'"));
if($result <= 32)
$result = "<blink class=\"blink_warning\">".$result."</blink>";
echo $result; ?>';
else
echo '<?php echo CleanSNMPResult(snmp2_get("'.$host->m_host.'", "'.$host->m_community.'", "'.$query->m_oid.".".(int)$subhost.'")); ?>';
break;
}
}
}
$config = array(
'hostname'=>$wwwHostname,
'graphs'=>$wwwPathGraphs
);
$conf = "<?php\n";
foreach($config as $var=>$val)
$conf .= '$' . $var . ' = "' . $val . '";' . "\n";
$conf .= "?>";
////////////////
//generate tsunami index
//create "back" link
$back = '//'.$wwwHostname;
//create host preview table rows
$content = "";
$cache = "";
foreach($aHosts as $host)
{
$content .= '<?php $host="' . $host->m_id . '"; ?>' . "\n";
$content .= '<?php openCache("' . $pathWWW . $wwwPathCache . $host->m_id . ".json" . '"); ?>' . "\n";
$content .= file_get_contents($pathTemplates.$host->m_templateHostPreview);
}
//insert table rows into the host preview page template
ob_start();
eval('?>'.file_get_contents($pathTemplates."mainIndex.php"));
$pageContent = ob_get_clean();
$pageTitle = "tsunami";
//insert the host preview page template into the main template
ob_start();
eval('?>'.file_get_contents($fileWWWTemplate));
$indexTemplate = ob_get_clean();
//write the file out, adding the SNMP result processing functions
$fileIndex = $pathWWW."index.php";
echo date("Ymd H:i:s")." Creating file \"$fileIndex\"\n";
$fhIndex = fopen($fileIndex, "w");
fwrite($fhIndex, file_get_contents($pathWeb.'cache.php'));
fwrite($fhIndex, $conf);
fwrite($fhIndex, $indexTemplate);
fclose($fhIndex);
////////////////
//generate host indices
//for each page
foreach($aHosts as $host)
{
//create "back" link
$back = '//'.$wwwHostname;
//create subhost preview table rows
$content = "";
foreach($aCollectedHosts[$host->m_id]->m_subhosts as $subhost)
{
$content .= '<?php $subhost="' . $subhost . '"; ?>' . "\n";
$content .= file_get_contents($pathTemplates.$host->m_templateSubhostPreview);
}
//insert subhost preview table rows into subhost preview template
$pageContent = file_get_contents($pathTemplates.$host->m_templateHostPage);
$pageContent = str_replace('{content}', $content, $pageContent);
$pageTitle = "tsunami | " .$host->m_id;
//insert subhost preview template into the main template
ob_start();
eval('?>'.file_get_contents($fileWWWTemplate));
$indexTemplate = ob_get_clean();
//make sure the host's directory exists
$pathHostPage = $pathWWW.$host->m_id."/";
if(!file_exists($pathHostPage))
{
mkdir($pathHostPage);
}
//write the file out
$fileIndex = $pathWWW.$host->m_id."/index.php";
echo date("Ymd H:i:s")." Creating file \"$fileIndex\"\n";
$fhIndex = fopen($fileIndex, "w");
fwrite($fhIndex, file_get_contents($pathWeb.'cache.php'));
fwrite($fhIndex, '<?php $host="' . $host->m_id . '"; ?>' . "\n");
fwrite($fhIndex, '<?php openCache("' . $pathWWW . $wwwPathCache . $host->m_id . ".json" . '"); ?>' . "\n");
fwrite($fhIndex, $conf);
fwrite($fhIndex, $indexTemplate);
fclose($fhIndex);
}
////////////////
//generate subhost indices
//for each page
foreach($aHosts as $host)
{
foreach($aCollectedHosts[$host->m_id]->m_subhosts as $subhost)
{
//create "back" link
$back = '//'.$wwwHostname.$host->m_id;
//create subhost page content
$pageContent = file_get_contents($pathTemplates.$host->m_templateSubhostPage);
$pageTitle = "tsunami | " .$host->m_id. " | ".$subhost;
//insert subhost preview template into the main template
ob_start();
eval('?>'.file_get_contents($fileWWWTemplate));
$indexTemplate = ob_get_clean();
//make sure the host's directory exists
$pathHostPage = $pathWWW.$host->m_id."/".$subhost."/";
if(!file_exists($pathHostPage))
{
mkdir($pathHostPage);
}
//write the file out
$fileIndex = $pathWWW.$host->m_id."/".$subhost."/index.php";
echo date("Ymd H:i:s")." Creating file \"$fileIndex\"\n";
$fhIndex = fopen($fileIndex, "w");
fwrite($fhIndex, file_get_contents($pathWeb.'cache.php'));
fwrite($fhIndex, '<?php $host="' . $host->m_id . '"; ?>' . "\n");
fwrite($fhIndex, '<?php $subhost="' . $subhost . '"; ?>' . "\n");
fwrite($fhIndex, '<?php openCache("' . $pathWWW . $wwwPathCache . $host->m_id . ".json" . '"); ?>' . "\n");
fwrite($fhIndex, $conf);
fwrite($fhIndex, $indexTemplate);
fclose($fhIndex);
}
}
echo date("Ymd H:i:s")." Done building viewer.\n";
?>