1# 2# Monitorix - A lightweight system monitoring tool. 3# 4# Copyright (C) 2005-2017 by Jordi Sanfeliu <jordi@fibranet.cat> 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License along 17# with this program; if not, write to the Free Software Foundation, Inc., 18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19# 20 21package ipmi; 22 23use strict; 24use warnings; 25use Monitorix; 26use RRDs; 27use POSIX qw(strftime); 28use Exporter 'import'; 29our @EXPORT = qw(ipmi_init ipmi_update ipmi_cgi); 30 31sub ipmi_init { 32 my $myself = (caller(0))[3]; 33 my ($package, $config, $debug) = @_; 34 my $rrd = $config->{base_lib} . $package . ".rrd"; 35 my $ipmi = $config->{ipmi}; 36 37 my $info; 38 my @ds; 39 my @rra; 40 my @tmp; 41 my $n; 42 43 my @average; 44 my @min; 45 my @max; 46 my @last; 47 48 if(-e $rrd) { 49 $info = RRDs::info($rrd); 50 for my $key (keys %$info) { 51 if(index($key, 'ds[') == 0) { 52 if(index($key, '.type') != -1) { 53 push(@ds, substr($key, 3, index($key, ']') - 3)); 54 } 55 } 56 if(index($key, 'rra[') == 0) { 57 if(index($key, '.rows') != -1) { 58 push(@rra, substr($key, 4, index($key, ']') - 4)); 59 } 60 } 61 } 62 if(scalar(@ds) / 9 != scalar(my @fl = split(',', $ipmi->{list}))) { 63 logger("$myself: Detected size mismatch between 'list' (" . scalar(my @fl = split(',', $ipmi->{list})) . ") and $rrd (" . scalar(@ds) / 9 . "). Resizing it accordingly. All historical data will be lost. Backup file created."); 64 rename($rrd, "$rrd.bak"); 65 } 66 if(scalar(@rra) < 12 + (4 * $config->{max_historic_years})) { 67 logger("$myself: Detected size mismatch between 'max_historic_years' (" . $config->{max_historic_years} . ") and $rrd (" . ((scalar(@rra) -12) / 4) . "). Resizing it accordingly. All historical data will be lost. Backup file created."); 68 rename($rrd, "$rrd.bak"); 69 } 70 } 71 72 if(!(-e $rrd)) { 73 logger("Creating '$rrd' file."); 74 for($n = 1; $n <= $config->{max_historic_years}; $n++) { 75 push(@average, "RRA:AVERAGE:0.5:1440:" . (365 * $n)); 76 push(@min, "RRA:MIN:0.5:1440:" . (365 * $n)); 77 push(@max, "RRA:MAX:0.5:1440:" . (365 * $n)); 78 push(@last, "RRA:LAST:0.5:1440:" . (365 * $n)); 79 } 80 for($n = 0; $n < scalar(my @sl = split(',', $ipmi->{list})); $n++) { 81 push(@tmp, "DS:ipmi" . $n . "_s1:GAUGE:120:0:U"); 82 push(@tmp, "DS:ipmi" . $n . "_s2:GAUGE:120:0:U"); 83 push(@tmp, "DS:ipmi" . $n . "_s3:GAUGE:120:0:U"); 84 push(@tmp, "DS:ipmi" . $n . "_s4:GAUGE:120:0:U"); 85 push(@tmp, "DS:ipmi" . $n . "_s5:GAUGE:120:0:U"); 86 push(@tmp, "DS:ipmi" . $n . "_s6:GAUGE:120:0:U"); 87 push(@tmp, "DS:ipmi" . $n . "_s7:GAUGE:120:0:U"); 88 push(@tmp, "DS:ipmi" . $n . "_s8:GAUGE:120:0:U"); 89 push(@tmp, "DS:ipmi" . $n . "_s9:GAUGE:120:0:U"); 90 } 91 eval { 92 RRDs::create($rrd, 93 "--step=60", 94 @tmp, 95 "RRA:AVERAGE:0.5:1:1440", 96 "RRA:AVERAGE:0.5:30:336", 97 "RRA:AVERAGE:0.5:60:744", 98 @average, 99 "RRA:MIN:0.5:1:1440", 100 "RRA:MIN:0.5:30:336", 101 "RRA:MIN:0.5:60:744", 102 @min, 103 "RRA:MAX:0.5:1:1440", 104 "RRA:MAX:0.5:30:336", 105 "RRA:MAX:0.5:60:744", 106 @max, 107 "RRA:LAST:0.5:1:1440", 108 "RRA:LAST:0.5:30:336", 109 "RRA:LAST:0.5:60:744", 110 @last, 111 ); 112 }; 113 my $err = RRDs::error; 114 if($@ || $err) { 115 logger("$@") unless !$@; 116 if($err) { 117 logger("ERROR: while creating $rrd: $err"); 118 if($err eq "RRDs::error") { 119 logger("... is the RRDtool Perl package installed?"); 120 } 121 } 122 return; 123 } 124 } 125 126 $config->{ipmi_hist_alerts} = (); 127 push(@{$config->{func_update}}, $package); 128 logger("$myself: Ok") if $debug; 129} 130 131sub ipmi_update { 132 my $myself = (caller(0))[3]; 133 my ($package, $config, $debug) = @_; 134 my $rrd = $config->{base_lib} . $package . ".rrd"; 135 my $ipmi = $config->{ipmi}; 136 my $args = $ipmi->{extra_args} || ""; 137 138 my @sens; 139 140 my $n; 141 my $str; 142 my $rrdata = "N"; 143 144 if(!open(IN, "ipmitool $args sdr |")) { 145 logger("$myself: unable to execute 'ipmitool'. $!"); 146 return; 147 } 148 my @data = <IN>; 149 close(IN); 150 151 my $e = 0; 152 while($e < scalar(my @sl = split(',', $ipmi->{list}))) { 153 my $e2 = 0; 154 foreach my $i (split(',', $ipmi->{desc}->{$e})) { 155 my $unit; 156 157 $sens[$e][$e2] = 0 unless defined $sens[$e][$e2]; 158 $str = trim($i); 159 $unit = $ipmi->{units}->{$e}; 160 foreach(@data) { 161 if(/^($str)\s+\|\s+(\d+\.*\d*)\s+$unit\s+/) { 162 my $val = $2; 163 $sens[$e][$e2] = $val; 164 165 # check alerts for each sensor defined 166 $str =~ s/ /_/; 167 my @al = split(',', $ipmi->{alerts}->{$str} || ""); 168 if(scalar(@al)) { 169 my $timeintvl = trim($al[0]); 170 my $threshold = trim($al[1]); 171 my $script = trim($al[2]); 172 173 if(!$threshold || $val < $threshold) { 174 $config->{ipmi_hist_alerts}->{$str} = 0; 175 } else { 176 if(!$config->{ipmi_hist_alerts}->{$str}) { 177 $config->{ipmi_hist_alerts}->{$str} = time; 178 } 179 if($config->{ipmi_hist_alerts}->{$str} > 0 && (time - $config->{ipmi_hist_alerts}->{$str}) >= $timeintvl) { 180 if(-x $script) { 181 logger("$myself: alert on IPMI Sensor ($str): executing script '$script'."); 182 system($script . " " . $timeintvl . " " . $threshold . " " . $val); 183 } else { 184 logger("$myself: ERROR: script '$script' doesn't exist or don't has execution permissions."); 185 } 186 $config->{ipmi_hist_alerts}->{$str} = time; 187 } 188 } 189 } 190 } 191 } 192 $e2++; 193 } 194 $e++; 195 } 196 197 $e = 0; 198 while($e < scalar(my @sl = split(',', $ipmi->{list}))) { 199 for($n = 0; $n < 9; $n++) { 200 $sens[$e][$n] = 0 unless defined $sens[$e][$n]; 201 $rrdata .= ":" . $sens[$e][$n]; 202 } 203 $e++; 204 } 205 206 RRDs::update($rrd, $rrdata); 207 logger("$myself: $rrdata") if $debug; 208 my $err = RRDs::error; 209 logger("ERROR: while updating $rrd: $err") if $err; 210} 211 212sub ipmi_cgi { 213 my ($package, $config, $cgi) = @_; 214 my @output; 215 216 my $ipmi = $config->{ipmi}; 217 my @rigid = split(',', ($ipmi->{rigid} || "")); 218 my @limit = split(',', ($ipmi->{limit} || "")); 219 my $tf = $cgi->{tf}; 220 my $colors = $cgi->{colors}; 221 my $graph = $cgi->{graph}; 222 my $silent = $cgi->{silent}; 223 my $zoom = "--zoom=" . $config->{global_zoom}; 224 my %rrd = ( 225 'new' => \&RRDs::graphv, 226 'old' => \&RRDs::graph, 227 ); 228 my $version = "new"; 229 my $pic; 230 my $picz; 231 my $picz_width; 232 my $picz_height; 233 234 my $u = ""; 235 my $width; 236 my $height; 237 my @riglim; 238 my @IMG; 239 my @IMGz; 240 my @tmp; 241 my @tmpz; 242 my @CDEF; 243 my $n; 244 my $n2; 245 my $str; 246 my $err; 247 my @LC = ( 248 "#4444EE", 249 "#EEEE44", 250 "#44EEEE", 251 "#EE44EE", 252 "#888888", 253 "#E29136", 254 "#44EE44", 255 "#448844", 256 "#EE4444", 257 ); 258 259 $version = "old" if $RRDs::VERSION < 1.3; 260 my $rrd = $config->{base_lib} . $package . ".rrd"; 261 my $title = $config->{graph_title}->{$package}; 262 my $IMG_DIR = $config->{base_dir} . "/" . $config->{imgs_dir}; 263 my $imgfmt_uc = uc($config->{image_format}); 264 my $imgfmt_lc = lc($config->{image_format}); 265 266 $title = !$silent ? $title : ""; 267 268 269 # text mode 270 # 271 if(lc($config->{iface_mode}) eq "text") { 272 if($title) { 273 push(@output, main::graph_header($title, 2)); 274 push(@output, " <tr>\n"); 275 push(@output, " <td bgcolor='$colors->{title_bg_color}'>\n"); 276 } 277 my (undef, undef, undef, $data) = RRDs::fetch("$rrd", 278 "--start=-$tf->{nwhen}$tf->{twhen}", 279 "AVERAGE", 280 "-r $tf->{res}"); 281 $err = RRDs::error; 282 push(@output, "ERROR: while fetching $rrd: $err\n") if $err; 283 my $line1; 284 my $line2; 285 my $line3; 286 push(@output, " <pre style='font-size: 12px; color: $colors->{fg_color}';>\n"); 287 push(@output, " "); 288 for($n = 0; $n < scalar(my @sl = split(',', $ipmi->{list})); $n++) { 289 $line1 = ""; 290 foreach my $i (split(',', $ipmi->{desc}->{$n})) { 291 $i = trim($i); 292 $str = $ipmi->{map}->{$i} || $i; 293 $str = sprintf("%7s", substr($str, 0, 5)); 294 $line1 .= " "; 295 $line2 .= sprintf(" %7s", $str); 296 $line3 .= "--------"; 297 } 298 if($line1) { 299 my $i = length($line1); 300 push(@output, sprintf(sprintf("%${i}s", sprintf("%s", trim($sl[$n]))))); 301 } 302 } 303 push(@output, "\n"); 304 push(@output, "Time$line2\n"); 305 push(@output, "----$line3 \n"); 306 my $line; 307 my $time; 308 my $n2; 309 my $n3; 310 my $from; 311 my $to; 312 for($n = 0, $time = $tf->{tb}; $n < ($tf->{tb} * $tf->{ts}); $n++) { 313 $line = @$data[$n]; 314 $time = $time - (1 / $tf->{ts}); 315 push(@output, sprintf(" %2d$tf->{tc} ", $time)); 316 for($n2 = 0; $n2 < scalar(my @sl = split(',', $ipmi->{list})); $n2++) { 317 $n3 = $n2 * 9; 318 foreach my $i (split(',', $ipmi->{desc}->{$n2})) { 319 $from = $n3++; 320 $to = $from + 1; 321 my ($j) = @$line[$from..$to]; 322 push(@output, sprintf("%7.1lf ", $j || 0)); 323 } 324 } 325 push(@output, "\n"); 326 } 327 push(@output, " </pre>\n"); 328 if($title) { 329 push(@output, " </td>\n"); 330 push(@output, " </tr>\n"); 331 push(@output, main::graph_footer()); 332 } 333 push(@output, " <br>\n"); 334 return @output; 335 } 336 337 338 # graph mode 339 # 340 if($silent eq "yes" || $silent eq "imagetag") { 341 $colors->{fg_color} = "#000000"; # visible color for text mode 342 $u = "_"; 343 } 344 if($silent eq "imagetagbig") { 345 $colors->{fg_color} = "#000000"; # visible color for text mode 346 $u = ""; 347 } 348 349 for($n = 0; $n < scalar(my @sl = split(',', $ipmi->{list})); $n++) { 350 $str = $u . $package . $n . "." . $tf->{when} . ".$imgfmt_lc"; 351 push(@IMG, $str); 352 unlink("$IMG_DIR" . $str); 353 if(lc($config->{enable_zoom}) eq "y") { 354 $str = $u . $package . $n . "z." . $tf->{when} . ".$imgfmt_lc"; 355 push(@IMGz, $str); 356 unlink("$IMG_DIR" . $str); 357 } 358 } 359 360 @riglim = @{setup_riglim($rigid[0], $limit[0])}; 361 $n = 0; 362 while($n < scalar(my @sl = split(',', $ipmi->{list}))) { 363 if($title) { 364 if($n == 0) { 365 push(@output, main::graph_header($title, $ipmi->{graphs_per_row})); 366 } 367 push(@output, " <tr>\n"); 368 } 369 for($n2 = 0; $n2 < $ipmi->{graphs_per_row}; $n2++) { 370 last unless $n < scalar(my @sl = split(',', $ipmi->{list})); 371 if($title) { 372 push(@output, " <td bgcolor='" . $colors->{title_bg_color} . "'>\n"); 373 } 374 undef(@tmp); 375 undef(@tmpz); 376 undef(@CDEF); 377 my $e = 0; 378 my $unit = $ipmi->{units}->{$n}; 379 foreach my $i (split(',', $ipmi->{desc}->{$n})) { 380 $i = trim($i); 381 $str = $ipmi->{map}->{$i} || $i; 382 $str = sprintf("%-40s", substr($str, 0, 40)); 383 push(@tmp, "LINE2:s" . ($e + 1) . $LC[$e] . ":$str"); 384 push(@tmp, "GPRINT:s" . ($e + 1) . ":LAST: Current\\:%7.1lf\\n"); 385 push(@tmpz, "LINE2:s" . ($e + 1) . $LC[$e] . ":$str"); 386 $e++; 387 } 388 while($e < 9) { 389 push(@tmp, "COMMENT: \\n"); 390 $e++; 391 } 392 if(lc($config->{show_gaps}) eq "y") { 393 push(@tmp, "AREA:wrongdata#$colors->{gap}:"); 394 push(@tmpz, "AREA:wrongdata#$colors->{gap}:"); 395 push(@CDEF, "CDEF:wrongdata=allvalues,UN,INF,UNKN,IF"); 396 } 397 ($width, $height) = split('x', $config->{graph_size}->{medium}); 398 $str = substr(trim($sl[$n]), 0, 25); 399 $pic = $rrd{$version}->("$IMG_DIR" . "$IMG[$n]", 400 "--title=$str ($tf->{nwhen}$tf->{twhen})", 401 "--start=-$tf->{nwhen}$tf->{twhen}", 402 "--imgformat=$imgfmt_uc", 403 "--vertical-label=$unit", 404 "--width=$width", 405 "--height=$height", 406 @riglim, 407 $zoom, 408 @{$cgi->{version12}}, 409 @{$cgi->{version12_small}}, 410 @{$colors->{graph_colors}}, 411 "DEF:s1=$rrd:ipmi" . $n . "_s1:AVERAGE", 412 "DEF:s2=$rrd:ipmi" . $n . "_s2:AVERAGE", 413 "DEF:s3=$rrd:ipmi" . $n . "_s3:AVERAGE", 414 "DEF:s4=$rrd:ipmi" . $n . "_s4:AVERAGE", 415 "DEF:s5=$rrd:ipmi" . $n . "_s5:AVERAGE", 416 "DEF:s6=$rrd:ipmi" . $n . "_s6:AVERAGE", 417 "DEF:s7=$rrd:ipmi" . $n . "_s7:AVERAGE", 418 "DEF:s8=$rrd:ipmi" . $n . "_s8:AVERAGE", 419 "DEF:s9=$rrd:ipmi" . $n . "_s9:AVERAGE", 420 "CDEF:allvalues=s1,s2,s3,s4,s5,s6,s7,s8,s9,+,+,+,+,+,+,+,+", 421 @CDEF, 422 @tmp); 423 $err = RRDs::error; 424 push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$n]: $err\n") if $err; 425 if(lc($config->{enable_zoom}) eq "y") { 426 ($width, $height) = split('x', $config->{graph_size}->{zoom}); 427 $picz = $rrd{$version}->("$IMG_DIR" . "$IMGz[$n]", 428 "--title=$str ($tf->{nwhen}$tf->{twhen})", 429 "--start=-$tf->{nwhen}$tf->{twhen}", 430 "--imgformat=$imgfmt_uc", 431 "--vertical-label=$unit", 432 "--width=$width", 433 "--height=$height", 434 @riglim, 435 $zoom, 436 @{$cgi->{version12}}, 437 @{$cgi->{version12_small}}, 438 @{$colors->{graph_colors}}, 439 "DEF:s1=$rrd:ipmi" . $n . "_s1:AVERAGE", 440 "DEF:s2=$rrd:ipmi" . $n . "_s2:AVERAGE", 441 "DEF:s3=$rrd:ipmi" . $n . "_s3:AVERAGE", 442 "DEF:s4=$rrd:ipmi" . $n . "_s4:AVERAGE", 443 "DEF:s5=$rrd:ipmi" . $n . "_s5:AVERAGE", 444 "DEF:s6=$rrd:ipmi" . $n . "_s6:AVERAGE", 445 "DEF:s7=$rrd:ipmi" . $n . "_s7:AVERAGE", 446 "DEF:s8=$rrd:ipmi" . $n . "_s8:AVERAGE", 447 "DEF:s9=$rrd:ipmi" . $n . "_s9:AVERAGE", 448 "CDEF:allvalues=s1,s2,s3,s4,s5,s6,s7,s8,s9,+,+,+,+,+,+,+,+", 449 @CDEF, 450 @tmpz); 451 $err = RRDs::error; 452 push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$n]: $err\n") if $err; 453 } 454 if($title || ($silent =~ /imagetag/ && $graph =~ /ipmi$n/)) { 455 if(lc($config->{enable_zoom}) eq "y") { 456 if(lc($config->{disable_javascript_void}) eq "y") { 457 push(@output, " <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$n] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$n] . "' border='0'></a>\n"); 458 } else { 459 if($version eq "new") { 460 $picz_width = $picz->{image_width} * $config->{global_zoom}; 461 $picz_height = $picz->{image_height} * $config->{global_zoom}; 462 } else { 463 $picz_width = $width + 115; 464 $picz_height = $height + 100; 465 } 466 push(@output, " <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$n] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$n] . "' border='0'></a>\n"); 467 } 468 } else { 469 push(@output, " <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$n] . "'>\n"); 470 } 471 } 472 if($title) { 473 push(@output, " </td>\n"); 474 } 475 $n++; 476 } 477 if($title) { 478 push(@output, " </tr>\n"); 479 } 480 } 481 if($title) { 482 push(@output, main::graph_footer()); 483 } 484 push(@output, " <br>\n"); 485 return @output; 486} 487 4881; 489