jablonka.czprosek.czf

weathermap

Subversion Repositories:
[/] [lib/] [poller-common.php] - Blame information for rev 22

 

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

Powered by WebSVN 2.2.1