forked from howardjones/network-weathermap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweathermap
executable file
·301 lines (252 loc) · 10.6 KB
/
weathermap
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
#!/usr/bin/php
<?php
// PHP Weathermap 0.98
// Copyright Howard Jones, 2005-2016 [email protected]
// http://www.network-weathermap.com/
// Released under the GNU Public License
use GetOpt\GetOpt;
use GetOpt\Option;
use GetOpt\ArgumentException;
use Weathermap\Core\Map;
use Weathermap\Core\MapUtility;
require_once __DIR__ . '/lib/all.php';
// **************************************************************************************
// 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)
// **************************************************************************************
if (!Weathermap\Core\MapUtility::moduleChecks()) {
die("Quitting: Module checks failed.\n");
}
$getOpt = new \GetOpt\GetOpt(null, [\GetOpt\GetOpt::SETTING_STRICT_OPERANDS => true]);
$getOpt->addOptions(array(
Option::create(null, 'version', GetOpt::NO_ARGUMENT)
->setDescription('Show version info and quit'),
Option::create('h', 'help', GetOpt::NO_ARGUMENT)
->setDescription('Show this help and quit'),
Option::create(null, 'config', GetOpt::REQUIRED_ARGUMENT)
->setDescription('filename to read from. Default weathermap.conf')
->setArgumentName('filename')
->setDefaultValue('weathermap.conf'),
Option::create(null, 'output', GetOpt::REQUIRED_ARGUMENT)
->setDescription('filename to write image. Default weathermap.png')
->setArgumentName('filename')
->setDefaultValue('weathermap.png'),
Option::create(null, 'htmloutput', GetOpt::REQUIRED_ARGUMENT)
->setDescription('filename to write HTML. Default weathermap.html')
->setArgumentName('filename')
->setDefaultValue('weathermap.html'),
Option::create(null, 'image-uri', GetOpt::REQUIRED_ARGUMENT)
->setArgumentName('uri')
->setDescription('URI to prefix <img> tags in HTML output'),
Option::create(null, 'define', GetOpt::MULTIPLE_ARGUMENT)
->setArgumentName('name=value')
->setDescription('Define internal variables (equivalent to global SET in config file)'),
Option::create(null, 'stats', GetOpt::NO_ARGUMENT)
->setDescription('produce statistics for map after completion'),
Option::create(null, 'bulge', GetOpt::NO_ARGUMENT)
->setDescription('Enable link-bulging mode. See manual.'),
Option::create(null, 'no-data', GetOpt::NO_ARGUMENT)
->setDescription('skip the data-reading process (just a \'grey\' map)'),
Option::create(null, 'randomdata', GetOpt::NO_ARGUMENT)
->setDescription('skip the data-reading process, generate random data'),
Option::create(null, 'debug', GetOpt::NO_ARGUMENT)
->setDescription('produce (LOTS) of debugging information during run'),
Option::create(null, 'no-warn', GetOpt::REQUIRED_ARGUMENT)
->setDescription('suppress warnings with listed errorcodes (comma-separated)')
->setArgumentName('WMxxx_errorcode'),
Option::create(null, 'dump-after', GetOpt::NO_ARGUMENT)
->setDescription('(development) dump all internal PHP structures (HUGE)'),
Option::create(null, 'uberdebug', GetOpt::NO_ARGUMENT)
->setDescription('produce even more debug information'),
Option::create(null, 'setdebug', GetOpt::NO_ARGUMENT)
->setDescription('produce debug information related to map variables (SET)'),
Option::create(null, 'dump-config', GetOpt::REQUIRED_ARGUMENT)
->setArgumentName('filename')
->setDescription('(development) dump config to a new file (testing editor)'),
Option::create(null, 'dump-json', GetOpt::REQUIRED_ARGUMENT)
->setArgumentName('filename')
->setDescription('(development) dump JSON config to a new file'),
)
);
// process arguments and catch user errors
try {
$getOpt->process();
} catch (ArgumentException $exception) {
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
echo PHP_EOL . $getOpt->getHelpText();
exit;
}
// show version and quit
if ($getOpt->getOption('version')) {
echo sprintf('PHP Network Weathermap: %s' . PHP_EOL, \Weathermap\Core\WEATHERMAP_VERSION);
exit;
}
// show help and quit
if ($getOpt->getOption('help')) {
echo $getOpt->getHelpText();
exit;
}
$options_output = array();
$defines = array();
$configfile = $getOpt->getOption('config');
$htmlfile = $getOpt->getOption('htmloutput');
$imagefile = $getOpt->getOption('output');
$options_output['imageuri'] = $getOpt->getOption('image-uri');
if ($getOpt->getOption('bulge') === 1) {
$options_output['widthmod'] = true;
}
if ($getOpt->getOption('sizedebug') === 1) {
$options_output['sizedebug'] = true;
}
if ($getOpt->getOption('no-data') === 1) {
$options_output['sizedebug'] = true;
}
if ($getOpt->getOption('debug') === 1) {
$options_output['debugging'] = true;
}
if ($getOpt->getOption('uberdebug') === 1) {
$options_output['debugging'] = true;
// allow ALL trace messages (normally we block some of the chatty ones)
$weathermap_debug_suppress = array();
}
if ($getOpt->getOption('no-warn') != '') {
// allow disabling of warnings from the command-line, too (mainly for the rrdtool warning)
$suppress_warnings = explode(",", $getOpt->getOption('no-warn'));
foreach ($suppress_warnings as $s) {
$weathermap_error_suppress[] = strtoupper($s);
}
}
$define_array = $getOpt->getOption('define');
foreach ($define_array as $define) {
preg_match("/^([^=]+)=(.*)\s*$/", $define, $matches);
if (isset($matches[2])) {
$varname = $matches[1];
$value = $matches[2];
MapUtility::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";
}
}
// 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;
$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');
MapUtility::debug("------------------------------------\n");
MapUtility::debug("Starting PHP-Weathermap run, with config: $configfile\n");
MapUtility::debug("------------------------------------\n");
}
$map = new Map();
$map->rrdtool = $rrdtool;
$map->context = "cli";
// 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->addHint($hintname, $hint);
}
// now stuff in all the others, that we got from getopts
foreach ($options_output as $key => $value) {
// $map->$key = $value;
$map->addHint($key, $value);
}
if ((isset($options_output['sizedebug']) && !$options_output['sizedebug']) || (!isset($options_output['sizedebug']))) {
if ($getOpt->getOption('randomdata') === 1) {
$map->randomData();
} else {
$map->ReadData();
}
}
if ($imagefile != '') {
$map->drawMap($imagefile);
$map->imagefile = $imagefile;
}
if ($htmlfile != '') {
MapUtility::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>');
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 ($getOpt->getOption('dump-config') != '') {
$map->writeConfig($getOpt->getOption('dump-config'));
}
if ($getOpt->getOption('dump-json') != '') {
$fd = fopen($getOpt->getOption('dump-json'), "w");
fputs($fd, $map->getJSONConfig());
fclose($fd);
}
if ($getOpt->getOption('stats') === 1) {
$map->DumpStats();
}
if ($map->dataoutputfile != '') {
$map->writeDataFile($map->dataoutputfile);
}
if ($getOpt->getOption('dump-after') === 1) {
print_r($map);
}
if ($getOpt->getOption('setdebug') === 1) {
foreach ($map->buildAllItemsList() as $item) {
print "$item->name :\n";
foreach ($item->hints as $n => $v) {
print " SET $n = $v\n";
}
foreach ($item->notes as $n => $v) {
print " -> $n = $v\n";
}
print "\n";
}
}
} 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: