Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / pnp-templates / default.php
blob59bbbe7093768f07fffa5be219ff2c1b0f5ecf66
1 <?php
3 # Fetch dynamic PNP template from Check_MK's new metrics system
5 # avoid redeclaration errors if this file is include multiple times (e.g. by basket)
6 if (!function_exists('get_apache_port')) {
7 function get_apache_port() {
8 $path = getenv("OMD_ROOT") . "/etc/omd/site.conf";
9 foreach (file($path) as $line) {
10 if (strpos($line, "CONFIG_APACHE_TCP_PORT") === 0) {
11 list($key, $val) = explode("=", $line);
12 return trim($val, "'\n\r");
18 $omd_site = getenv("OMD_SITE");
19 if ($omd_site) {
20 $url = "http://localhost:".get_apache_port()."/$omd_site/check_mk/";
21 $template_cache_dir = getenv("OMD_ROOT") . "/var/check_mk/pnp_template_cache";
23 else {
24 $url = "http://localhost/check_mk/";
25 $template_cache_dir = "/tmp/check_mk_pnp_template_cache";
28 if (!file_exists($template_cache_dir))
29 mkdir($template_cache_dir, 0755, TRUE);
31 # Get the list of performance variables and convert them to a string,
32 # prepend the command name, # e.g. "check_mk-hr_fs:fs_trend,fs_used,zabelfoobar"
33 $perf_vars = Array();
34 foreach ($NAME as $i => $n) {
35 $perf_vars[] = $n;
37 sort($perf_vars);
38 # We have to separate the check command name from the rest on
39 # the right place, i.d. the first "!", to prevent errors while
40 # creating the default template, e.g. for the custom check
42 # check-mk-custom!$USER2$/check_mssql_health_new --server $HOSTNAME$ --username=$USER6$
43 # --password=$USER5$ --mode database-free --name filr --warning 20: --critical 10: --commit
45 # If no "!" exists then $CHECK_COMMAND = $NAGIOS_CHECK_COMMAND
46 $check_command_parts = explode("!", $NAGIOS_CHECK_COMMAND);
47 $id_string = $check_command_parts[0] . ":" . implode(",", $perf_vars);
49 # Get current state of previously cached template data for this ID
50 $template_cache_path = $template_cache_dir . "/" . md5($id_string);
51 if (file_exists($template_cache_path)) {
52 $age = time() - filemtime($template_cache_path);
53 if ($age < 60 * 10)
54 $cache_state = "uptodate";
55 else
56 $cache_state = "stale";
58 else
59 $cache_state = "missing";
61 # cache file missing or stale: try to fetch live template via HTTP
62 if ($cache_state != "uptodate")
64 // always speaking to local host, so a small connect timeout is good to
65 // catch to long hanging requests when e.g. the system apache is not
66 // listening on localhost
67 ini_set('default_socket_timeout', 2);
68 $fd = @fopen($url . "pnp_template.py?id=" . urlencode($id_string), "r");
69 if ($fd) {
70 $data = "";
71 while (!feof($fd)) {
72 $data .= fread($fd, 4096);
74 fclose($fd);
75 $fd = fopen($template_cache_path, "w");
76 fwrite($fd, $data);
77 fclose($fd);
78 $cache_state = "uptodate";
82 if (!function_exists("replace_cmk_expression")) {
83 function replace_cmk_expression($NAME, $MIN, $MAX, $WARN, $CRIT, $text)
85 # Replace expressions in the title. This not a full implementation of
86 # the complete RPN expression syntax of Check_MK - but sufficient for
87 # all used cases.
88 foreach ($NAME as $i => $n) {
89 $text = str_replace("%($n:min@count)", "$MIN[$i]", $text);
90 $text = str_replace("%($n:max@count)", "$MAX[$i]", $text);
91 $text = str_replace("%($n:warn@count)", "$WARN[$i]", $text);
92 $text = str_replace("%($n:crit@count)", "$CRIT[$i]", $text);
94 return $text;
98 # Now read template information from cache file, if present
99 if (($cache_state == "uptodate" || $cache_state == "stale") && filesize($template_cache_path) > 0) {
100 $rrdbase = substr($NAGIOS_XMLFILE, 0, strlen($NAGIOS_XMLFILE) - 4);
101 $fd = fopen($template_cache_path, "r");
102 while (!feof($fd)) {
103 $dsname_line = trim(fgets($fd));
104 $option_line = replace_cmk_expression($NAME, $MIN, $MAX, $WARN, $CRIT, trim(fgets($fd)));
105 $graph_line = str_replace('$RRDBASE$', $rrdbase, fgets($fd));
106 if ($dsname_line && $option_line && $graph_line) {
107 $ds_name[] = $dsname_line;
108 $opt[] = $option_line;
109 $def[] = $graph_line;
112 fclose($fd);
116 # PNP Default template starts here...
118 # Copyright (c) 2006-2010 Joerg Linge (http://www.pnp4nagios.org)
119 # Default Template used if no other template is found.
120 # Don`t delete this file !
122 # Define some colors ..
124 else
126 $_WARNRULE = '#FFFF00';
127 $_CRITRULE = '#FF0000';
128 $_AREA = '#256aef';
129 $_LINE = '#000000';
131 # Initial Logic ...
134 foreach ($this->DS as $KEY=>$VAL) {
136 $maximum = "";
137 $minimum = "";
138 $critical = "";
139 $crit_min = "";
140 $crit_max = "";
141 $warning = "";
142 $warn_max = "";
143 $warn_min = "";
144 $vlabel = " ";
145 $lower = "";
146 $upper = "";
148 if ($VAL['WARN'] != "" && is_numeric($VAL['WARN']) ){
149 $warning = $VAL['WARN'];
151 if ($VAL['WARN_MAX'] != "" && is_numeric($VAL['WARN_MAX']) ) {
152 $warn_max = $VAL['WARN_MAX'];
154 if ( $VAL['WARN_MIN'] != "" && is_numeric($VAL['WARN_MIN']) ) {
155 $warn_min = $VAL['WARN_MIN'];
157 if ( $VAL['CRIT'] != "" && is_numeric($VAL['CRIT']) ) {
158 $critical = $VAL['CRIT'];
160 if ( $VAL['CRIT_MAX'] != "" && is_numeric($VAL['CRIT_MAX']) ) {
161 $crit_max = $VAL['CRIT_MAX'];
163 if ( $VAL['CRIT_MIN'] != "" && is_numeric($VAL['CRIT_MIN']) ) {
164 $crit_min = $VAL['CRIT_MIN'];
166 if ( $VAL['MIN'] != "" && is_numeric($VAL['MIN']) ) {
167 $lower = " --lower-limit=" . $VAL['MIN'];
168 $minimum = $VAL['MIN'];
170 if ( $VAL['MAX'] != "" && is_numeric($VAL['MAX']) ) {
171 $maximum = $VAL['MAX'];
173 if ($VAL['UNIT'] == "%%") {
174 $vlabel = "%";
175 $upper = " --upper-limit=101 ";
176 $lower = " --lower-limit=0 ";
178 else {
179 $vlabel = $VAL['UNIT'];
182 $opt[$KEY] = '--vertical-label "' . $vlabel . '" --title "' . $this->MACRO['DISP_HOSTNAME'] . ' / ' . $this->MACRO['DISP_SERVICEDESC'] . '"' . $upper . $lower;
183 $ds_name[$KEY] = $VAL['LABEL'];
184 $def[$KEY] = rrd::def ("var1", $VAL['RRDFILE'], $VAL['DS'], "AVERAGE");
185 $def[$KEY] .= rrd::gradient("var1", "3152A5", "BDC6DE", rrd::cut($VAL['NAME'],16), 20);
186 $def[$KEY] .= rrd::line1 ("var1", $_LINE );
187 $def[$KEY] .= rrd::gprint ("var1", array("LAST","MAX","AVERAGE"), "%3.4lf %S".$VAL['UNIT']);
188 if ($warning != "") {
189 $def[$KEY] .= rrd::hrule($warning, $_WARNRULE, "Warning $warning \\n");
191 if ($warn_min != "") {
192 $def[$KEY] .= rrd::hrule($warn_min, $_WARNRULE, "Warning (min) $warn_min \\n");
194 if ($warn_max != "") {
195 $def[$KEY] .= rrd::hrule($warn_max, $_WARNRULE, "Warning (max) $warn_max \\n");
197 if ($critical != "") {
198 $def[$KEY] .= rrd::hrule($critical, $_CRITRULE, "Critical $critical \\n");
200 if ($crit_min != "") {
201 $def[$KEY] .= rrd::hrule($crit_min, $_CRITRULE, "Critical (min) $crit_min \\n");
203 if ($crit_max != "") {
204 $def[$KEY] .= rrd::hrule($crit_max, $_CRITRULE, "Critical (max) $crit_max \\n");
206 $def[$KEY] .= rrd::comment("Default Template\\r");
207 $def[$KEY] .= rrd::comment("Command " . $VAL['TEMPLATE'] . "\\r");