-
Notifications
You must be signed in to change notification settings - Fork 52
/
weathermap.php
executable file
·358 lines (289 loc) · 9.84 KB
/
weathermap.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
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
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/php
<?php
// PHP Weathermap 0.98b
// Copyright Howard Jones, 2005-2020 howie@thingy.com
// http://www.network-weathermap.com/
// Released under the MIT License
require_once 'Console/Getopt.php';
require_once "lib/Weathermap.class.php";
include_once 'config.inc.php';
if (!wm_module_checks()) { die ("Quitting: Module checks failed.\n"); }
// $weathermap_debugging=TRUE;
$output="html";
$configfile="weathermap.conf";
$htmlfile='';
$imagefile='';
$dumpafter=0;
$dumpstats=0;
$randomdata=0;
$dumpconfig='';
$defines=array();
$daemon=0;
$daemon_args="";
// **************************************************************************************
// THIS IS THE ONE LINE IN HERE YOU MIGHT HAVE TO CHANGE!
$rrdtool="/usr/bin/rrdtool";
// (on Windows, use / instead of \ in pathnames - c:/rrdtool/bin/rrdtool.exe for example)
// **************************************************************************************
// initialize object
$cg=new Console_Getopt();
$short_opts='';
$long_opts=array
(
"version",
"help",
"image-uri=",
"base-href=",
"config=",
"output=",
"debug",
"uberdebug",
"stats",
"define=",
"no-data",
"randomdata",
"htmloutput=",
"dumpafter",
"bulge",
"sizedebug",
"dumpconfig=",
"daemon=",
"chdir="
);
$args=$cg->readPHPArgv();
$ret=$cg->getopt($args, $short_opts, $long_opts);
$pear = new PEAR;
if ($pear->isError($ret)) { die ("Error in command line: " . $ret->getMessage() . "\n (try --help)\n"); }
$gopts=$ret[0];
$options_output = array();
if (sizeof($gopts) > 0)
{
foreach ($gopts as $o)
{
switch ($o[0])
{
case '--config':
$configfile=$o[1];
break;
case '--htmloutput':
$htmlfile=$o[1];
break;
case '--dumpafter':
$dumpafter=1;
break;
case '--image-uri':
// $map->imageuri=$o[1];
$options_output['imageuri'] = $o[1];
break;
case '--base-href':
// $map->basehref=$o[1];
$options_output['basehref'] = $o[1];
break;
case '--dumpconfig':
//$map->dumpconfig=$o[1];
// $options_output['dumpconfig'] = $o[1];
$dumpconfig=$o[1];
break;
case '--randomdata':
$randomdata=1;
break;
case '--stats':
$dumpstats=1;
break;
case '--no-warn':
// allow disabling of warnings from the command-line, too (mainly for the rrdtool warning)
$suppress_warnings = explode(",", $o[1]);
foreach ($suppress_warnings as $s) {
$weathermap_error_suppress[] = strtoupper($s);
}
break;
case '--uberdebug':
// allow ALL trace messages (normally we block some of the chatty ones)
$weathermap_debug_suppress=array();
// FALL THROUGH
case '--debug':
$options_output['debugging'] = TRUE;
$weathermap_debugging=TRUE;
// enable assertion handling
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// Set up the callback
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
break;
case '--sizedebug':
case '--no-data':
//$map->sizedebug=TRUE;
$options_output['sizedebug'] = TRUE;
break;
case '--bulge':
//$map->widthmod=TRUE;
$options_output['widthmod'] = TRUE;
break;
case '--output':
$imagefile=$o[1];
break;
case '--daemon':
$daemon=1;
$daemon_args=$o[1];
#$rrdtool = $rrdtool . " --daemon ".$o[1];
break;
case '--chdir':
$chdir = $o[1];
break;
case '--define':
preg_match("/^([^=]+)=(.*)\s*$/",$o[1],$matches);
if(isset($matches[2]))
{
$varname = $matches[1];
$value = $matches[2];
wm_debug(">> $varname = '$value'\n");
// save this for later, so that when the map object exists, it can be defined
$defines[$varname]=$value;
}
else
{
print "WARNING: --define format is: --define name=value\n";
}
break;
case '--version':
print 'PHP Network Weathermap v' . $WEATHERMAP_VERSION."\n";
exit();
break;
case '--help':
print 'PHP Network Weathermap v' . $WEATHERMAP_VERSION."\n";
print "Copyright Howard Jones, 2005-2020 howie@thingy.com\nReleased under the MIT License\nhttp://www.network-weathermap.com/\n\n";
print "Usage: php weathermap [options]\n\n";
print " --config {filename} - filename to read from. Default weathermap.conf\n";
print " --output {filename} - filename to write image. Default weathermap.png\n";
print " --htmloutput {filename} - filename to write HTML. Default weathermap.html\n\n";
print " --base-href {uri} - URI for Weathermap, i.e /weathermap/\n";
print " --image-uri {uri} - URI to prefix <img> tags in HTML.\n";
print " --bulge - Enable link-bulging mode. See manual.\n\n";
print " --define name=value - Define internal variables\n";
print " (equivalent to global SET in config file)\n\n";
print " --no-data - skip the data-reading process (just a 'grey' map)\n";
print " --randomdata - as above, but use random data\n";
print " --debug - produce (LOTS) of debugging information during run\n";
print " --dump-after - dump all internal PHP structures (HUGE)\n";
print " --dumpconfig {filename} - filename to write a new config to (for testing)\n\n";
print " --help - show this help\n";
print " --version - show version number\n\n";
print " --daemon {path} - path to rrdcached sock\n\n";
print " --chdir {path} - path to change to before running rrdtool\n\n";
print "More info at http://www.network-weathermap.com/\n";
exit();
break;
}
}
}
// set this BEFORE we create the map object, so we get the debug output from Reset(), as well
if(isset($options_output['debugging']) && $options_output['debugging'])
{
$weathermap_debugging=TRUE;
wm_debug("------------------------------------\n");
wm_debug("Starting PHP-Weathermap run, with config: $configfile\n");
wm_debug("------------------------------------\n");
}
$map=new Weathermap;
$map->rrdtool = $rrdtool;
$map->context="cli";
$map->daemon=$daemon;
$map->daemon_args=$daemon_args;
if(isset($chdir)) {
$map->chdir=$chdir;
}
// now stuff in all the others, that we got from getopts
foreach ($options_output as $key=>$value)
{
$map->$key = $value;
// $map->add_hint($key,$value);
}
$weathermap_map = $configfile;
if ($map->ReadConfig($configfile))
{
// allow command-lines to override the config file, but provide a default if neither are present
if ($imagefile == '')
{
if ($map->imageoutputfile == '') { $imagefile="weathermap.png"; }
else { $imagefile=$map->imageoutputfile; }
}
if ($htmlfile == '')
{
if ($map->htmloutputfile != '') { $htmlfile = $map->htmloutputfile; }
}
// feed in any command-line defaults, so that they appear as if SET lines in the config
// XXX FIXME
foreach ($defines as $hintname=>$hint)
{
$map->add_hint($hintname,$hint);
}
// now stuff in all the others, that we got from getopts
foreach ($options_output as $key=>$value)
{
// $map->$key = $value;
$map->add_hint($key,$value);
}
// Get rrd default path
$rrdbase = $map->get_hint('rrd_default_path');
// Added the rrd_default_path if cannot be found
if($rrdbase == '') {
$rrdbase = isset($chdir) ? $chdir : $rrd_default_path1;
$map->add_hint('rrd_default_path', $rrdbase);
}
if ( (isset($options_output['sizedebug']) && ! $options_output['sizedebug']) || (!isset($options_output['sizedebug'])) )
{
if ($randomdata == 1) { $map->RandomData(); }
else { $map->ReadData(); }
}
# exit();
if ($imagefile != '')
{
$map->DrawMap($imagefile);
$map->imagefile=$imagefile;
}
if ($htmlfile != '')
{
wm_debug("Writing HTML to $htmlfile\n");
$fd=fopen($htmlfile, 'w');
fwrite($fd,
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>');
fwrite($fd,"<base href=\"$map->basehref\">");
if($map->htmlstylesheet != '') fwrite($fd,'<link rel="stylesheet" type="text/css" href="'.$map->htmlstylesheet.'" />');
fwrite($fd,'<meta http-equiv="refresh" content="300" /><title>' . $map->ProcessString($map->title, $map) . '</title></head><body>');
if ($map->htmlstyle == "overlib")
{
fwrite($fd,
"<div id=\"overDiv\" style=\"position:absolute; visibility:hidden; z-index:1000;\"></div>\n");
fwrite($fd,
"<script type=\"text/javascript\" src=\"overlib.js\"><!-- overLIB (c) Erik Bosrup --></script> \n");
}
fwrite($fd, $map->MakeHTML());
fwrite($fd,
'<hr /><span id="byline">Network Map created with <a href="http://www.network-weathermap.com/?vs='
. $WEATHERMAP_VERSION . '">PHP Network Weathermap v' . $WEATHERMAP_VERSION
. '</a></span></body></html>');
fclose ($fd);
}
if ($dumpconfig != '')
$map->WriteConfig($dumpconfig);
if ($dumpstats != '')
$map->DumpStats();
if ($dumpafter == 1)
print_r ($map);
# print_r ($map->node_template_tree);
# print_r ($map->link_template_tree);
# $map->cachefolder="editcache";
# $map->CacheUpdate(1);
}
else { die ("\n\nCould not read Weathermap config file. No output produced. Maybe try --help?\n"); }
function my_assert_handler($file, $line, $code)
{
echo "Assertion Failed:
File $file
Line $line
Code $code";
debug_print_backtrace();
exit();
}
// vim:ts=4:sw=4: