1 | 1 | simandl | <? |
2 | | | // common code used by the poller, the manual-run from the Cacti UI, and from the command-line manual-run. |
3 | | | // this is the easiest way to keep it all consistent! |
4 | | | |
5 | | | function weathermap_run_maps($mydir) { |
6 | | | global $config; |
7 | | | global $weathermap_debugging, $WEATHERMAP_VERSION; |
8 | | | |
9 | | | include_once($mydir.DIRECTORY_SEPARATOR."HTML_ImageMap.class.php"); |
10 | | | include_once($mydir.DIRECTORY_SEPARATOR."Weathermap.class.php"); |
11 | | | |
12 | | | $outdir = $mydir.DIRECTORY_SEPARATOR.'output'; |
13 | | | $confdir = $mydir.DIRECTORY_SEPARATOR.'configs'; |
14 | | | |
15 | | | $mapcount = 0; |
16 | | | |
17 | | | // take our debugging cue from the poller - turn on Poller debugging to get weathermap debugging |
18 | | | if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_DEBUG) |
19 | | | { |
20 | | | $weathermap_debugging = TRUE; |
21 | | | $mode_message = "DEBUG mode is on"; |
22 | | | } |
23 | | | else |
24 | | | { |
25 | | | $mode_message = "Normal logging mode. Turn on DEBUG in Cacti for more information"; |
26 | | | } |
27 | | | $quietlogging = read_config_option("weathermap_quiet_logging"); |
28 | | | // moved this outside the module_checks, so there should always be something in the logs! |
29 | | | if($quietlogging==0) cacti_log("Weathermap $WEATHERMAP_VERSION starting - $mode_message",true,"WEATHERMAP"); |
30 | | | |
31 | | | if(module_checks()) |
32 | | | { |
33 | | | |
34 | | | // move to the weathermap folder so all those relatives paths don't *have* to be absolute |
35 | | | $orig_cwd = getcwd(); |
36 | | | chdir($mydir); |
37 | | | |
38 | | | // first, see if the output directory even exists |
39 | | | if(is_dir($outdir)) |
40 | | | { |
41 | | | // next, make sure that we stand a chance of writing files |
42 | | | //// $testfile = realpath($outdir."weathermap.permissions.test"); |
43 | | | $testfile = $outdir.DIRECTORY_SEPARATOR."weathermap.permissions.test"; |
44 | | | $testfd = fopen($testfile, 'w'); |
45 | | | if($testfd) |
46 | | | { |
47 | | | fclose($testfd); |
48 | | | unlink($testfile); |
49 | | | |
50 | | | $queryrows = db_fetch_assoc("select * from weathermap_maps where active='on' order by sortorder,id"); |
51 | | | |
52 | | | if( is_array($queryrows) ) |
53 | | | { |
54 | | | debug("Iterating all maps."); |
55 | | | |
56 | | | $imageformat = strtolower(read_config_option("weathermap_output_format")); |
57 | | | |
58 | | | foreach ($queryrows as $map) { |
59 | | | $mapfile = $confdir.DIRECTORY_SEPARATOR.$map['configfile']; |
60 | | | $htmlfile = $outdir.DIRECTORY_SEPARATOR."weathermap_".$map['id'].".html"; |
61 | | | $imagefile = $outdir.DIRECTORY_SEPARATOR."weathermap_".$map['id'].".".$imageformat; |
62 | | | $thumbimagefile = $outdir.DIRECTORY_SEPARATOR."weathermap_thumb_".$map['id'].".".$imageformat; |
63 | | | |
64 | | | if(file_exists($mapfile)) |
65 | | | { |
66 | | | if($quietlogging==0) warn("Map: $mapfile -> $htmlfile & $imagefile"); |
67 | | | |
68 | | | $wmap = new Weathermap; |
69 | | | $wmap->context = "cacti"; |
70 | | | |
71 | | | // we can grab the rrdtool path from Cacti's config, in this case |
72 | | | $wmap->rrdtool = read_config_option("path_rrdtool"); |
73 | | | |
74 | | | $wmap->ReadConfig($mapfile); |
75 | | | $wmap->ReadData(); |
76 | | | |
77 | | | // $wmap->imageuri = $config['url_path'].'/plugins/weathermap/output/weathermap_'.$map['id'].".".$imageformat; |
78 | | | $wmap->imageuri = 'output/weathermap_'.$map['id'].".".$imageformat; |
79 | | | |
80 | | | if($quietlogging==0) warn("About to write image file. If this is the last message in your log, increase memory_limit in php.ini"); |
81 | | | if(function_exists("memory_get_usage")) |
82 | | | { |
83 | | | $mem_used = nice_bandwidth(memory_get_usage()); |
84 | | | $mem_allowed = ini_get("memory_limit"); |
85 | | | debug("memory_get_usage() says ".$mem_used."Bytes used. Limit is ".$mem_allowed."\n"); |
86 | | | } |
87 | | | |
88 | | | |
89 | | | $wmap->DrawMap($imagefile,$thumbimagefile,read_config_option("weathermap_thumbsize")); |
90 | | | |
91 | | | if($quietlogging==0) warn("Wrote map to $imagefile and $thumbimagefile"); |
92 | | | $fd = @fopen($htmlfile, 'w'); |
93 | | | if($fd != FALSE) |
94 | | | { |
95 | | | fwrite($fd, $wmap->MakeHTML('weathermap_'.$map['id'].'_imap')); |
96 | | | fclose($fd); |
97 | | | debug("Wrote HTML to $htmlfile"); |
98 | | | } |
99 | | | else |
100 | | | { |
101 | | | if(file_exists($htmlfile)) |
102 | | | { |
103 | | | warn("Failed to overwrite $htmlfile - permissions of existing file are wrong?\n"); |
104 | | | } |
105 | | | else |
106 | | | { |
107 | | | warn("Failed to create $htmlfile - permissions of output directory are wrong?\n"); |
108 | | | } |
109 | | | } |
110 | | | |
111 | | | db_execute("update weathermap_maps set titlecache='".mysql_real_escape_string($wmap->title)."' where id=".$map['id']); |
112 | | | $mapcount++; |
113 | | | } |
114 | | | else |
115 | | | { |
116 | | | warn("Mapfile $mapfile is not readable or doesn't exist"); |
117 | | | } |
118 | | | } |
119 | | | debug("Iterated all $mapcount maps."); |
120 | | | } |
121 | | | else |
122 | | | { |
123 | | | if($quietlogging==0) warn("No activated maps found."); |
124 | | | } |
125 | | | } |
126 | | | else |
127 | | | { |
128 | | | warn("Output directory ($outdir) isn't writable (tried to create '$testfile'). No maps created. You probably need to make it writable by the poller process (like you did with the RRA directory)"); |
129 | | | } |
130 | | | } |
131 | | | else |
132 | | | { |
133 | | | warn("Output directory ($outdir) doesn't exist!. No maps created. You probably need to create that directory, and make it writable by the poller process (like you did with the RRA directory)"); |
134 | | | } |
135 | | | chdir($orig_cwd); |
136 | | | if($quietlogging==0) warn("Weathermap $WEATHERMAP_VERSION run complete - $mapcount maps were run"); |
137 | | | } |
138 | | | else |
139 | | | { |
140 | | | warn("Required modules for PHP Weathermap $WEATHERMAP_VERSION were not present. Not running."); |
141 | | | } |
142 | | | } |
143 | | | |
144 | | | |
145 | | | // vim:ts=4:sw=4: |
146 | | | ?> |