/usr/lib/pnp4nagios/libexec/process_perfdata.pl is in pnp4nagios-bin 0.6.16-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 | #!/usr/bin/perl
# nagios: -epn
## pnp4nagios–0.6.16
## Copyright (c) 2005-2010 Joerg Linge (http://www.pnp4nagios.org)
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
if( $< == 0 ){
print "dont try this as root \n";
exit 1;
}
use warnings;
use strict;
use POSIX;
use Getopt::Long;
use Time::HiRes qw(gettimeofday tv_interval);
use vars qw ( $TEMPLATE %NAGIOS $t0 $t1 $rt $delayed_write $rrdfile @ds_create $count $line $name $ds_update $dstype);
my %conf = (
TIMEOUT => 15,
CFG_DIR => "/etc/pnp4nagios/",
USE_RRDs => 1,
RRDPATH => "/var/lib/pnp4nagios/perfdata",
RRDTOOL => "/usr/bin/rrdtool",
RRD_STORAGE_TYPE => "SINGLE",
RRD_HEARTBEAT => 8640,
RRA_STEP => 60,
RRA_CFG => "/etc/pnp4nagios/rra.cfg",
STATS_DIR => "/var/log/pnp4nagios/stats",
LOG_FILE => "/var/log/pnp4nagios/perfdata.log",
LOG_FILE_MAX_SIZE => "10485760", #Truncate after 10MB
LOG_LEVEL => 0,
XML_ENC => "UTF-8",
XML_UPDATE_DELAY => 0, # Write XML only if file is older then XML_UPDATE_DELAY seconds
RRD_DAEMON_OPTS => "",
GEARMAN_HOST => "localhost:4730", # How many gearman worker childs to start
PREFORK => 2, # How many gearman worker childs to start
REQUESTS_PER_CHILD => 20000, # Restart after a given count of requests
ENCRYPTION => 1, # Decrypt mod_gearman packets
KEY => 'should_be_changed',
KEY_FILE => '/etc/pnp4nagios/secret.key',
UOM2TYPE => { 'c' => 'DERIVE', 'd' => 'DERIVE' },
);
my %const = (
XML_STRUCTURE_VERSION => "4",
VERSION => "0.6.16",
);
#
# Dont change anything below these lines ...
#
#
# "rrdtool create" Syntax
#
my @default_rrd_create = ( "RRA:AVERAGE:0.5:1:2880", "RRA:AVERAGE:0.5:5:2880", "RRA:AVERAGE:0.5:30:4320", "RRA:AVERAGE:0.5:360:5840", "RRA:MAX:0.5:1:2880", "RRA:MAX:0.5:5:2880", "RRA:MAX:0.5:30:4320", "RRA:MAX:0.5:360:5840", "RRA:MIN:0.5:1:2880", "RRA:MIN:0.5:5:2880", "RRA:MIN:0.5:30:4320", "RRA:MIN:0.5:360:5840", );
Getopt::Long::Configure('bundling');
my ( $opt_d, $opt_V, $opt_h, $opt_i, $opt_n, $opt_b, $opt_gm, $opt_pidfile,$opt_daemon );
my $opt_t = $conf{TIMEOUT}; # Default Timeout
my $opt_c = $conf{CFG_DIR} . "process_perfdata.cfg";
GetOptions(
"V" => \$opt_V,
"version" => \$opt_V,
"h" => \$opt_h,
"help" => \$opt_h,
"i" => \$opt_i,
"inetd" => \$opt_i,
"b=s" => \$opt_b,
"bulk=s" => \$opt_b,
"d=s" => \$opt_d,
"datatype=s" => \$opt_d,
"t=i" => \$opt_t,
"timeout=i" => \$opt_t,
"c=s" => \$opt_c,
"config=s" => \$opt_c,
"n" => \$opt_n,
"npcd" => \$opt_n,
"gearman:s" => \$opt_gm,
"daemon" => \$opt_daemon,
"pidfile=s" => \$opt_pidfile,
);
parse_config($opt_c);
$conf{'GLOBAL_RRD_STORAGE_TYPE'} = uc($conf{'RRD_STORAGE_TYPE'}); # store the initial value for later use
$opt_t = $conf{TIMEOUT}; # Reset Timeout from config file
my %stats = init_stats();
my $cypher;
#
# RRDs Perl Module Detection
#
if ( $conf{USE_RRDs} == 1 ) {
unless ( eval "use RRDs;1" ) {
$conf{USE_RRDs} = 0;
}
}
#
# Include Gearman modules if needed
#
if ( defined($opt_gm) ) {
unless ( eval "use Gearman::Worker;1" ) {
print "Perl module Gearman::Worker not found\n";
exit 1;
}
unless ( eval "use MIME::Base64;1" ) {
print "Perl module MIME::Base64 not found\n";
exit 1;
}
unless ( eval "use Crypt::Rijndael;1" ) {
print "Perl module Crypt::Rijndael not found\n";
exit 1;
}
}
print_help() if ($opt_h);
print_version() if ($opt_V);
init_signals();
my %children = (); # keys are current child process IDs
my $children = 0; # current number of children
if( ! defined($opt_gm) ){
#
# synchronos / bulk / npcd mode
#
main();
}else{
#
# Gearman worker main loop
#
print_log( "process_perfdata.pl-$const{VERSION} Gearman Worker Daemon", 0 );
if($opt_gm =~ /:\d+/ ){
$conf{'GEARMAN_HOST'} = $opt_gm;
}
if($conf{ENCRYPTION} == 1){
print_log( "Encryptions is enabled", 0 );
read_keyfile($conf{'KEY_FILE'});
# fill key up to 32 bytes
$conf{'KEY'} = substr($conf{'KEY'},0,32) . chr(0) x ( 32 - length( $conf{'KEY'} ) );
$cypher = Crypt::Rijndael->new( $conf{'KEY'}, Crypt::Rijndael::MODE_ECB() );
}
daemonize();
}
#
# Subs
#
# Main function to switch to the right mode.
sub main {
my $job = shift;
my $t0 = [gettimeofday];
my $t1;
my $rt;
my $lines = 0;
# Gearman Worker
if (defined $opt_gm) {
print_log( "Gearman Worker Job start", 1 );
%NAGIOS = parse_env($job->arg);
$lines = process_perfdata();
$t1 = [gettimeofday];
$rt = tv_interval $t0, $t1;
$stats{runtime} += $rt;
$stats{rows}++;
if( ( int $stats{timet} / 60 ) < ( int time / 60 )){
store_internals();
init_stats();
}
print_log( "Gearman job end (runtime ${rt}s) ...", 1 );
return 1;
} elsif ( $opt_b && !$opt_n ) {
# Bulk mode
alarm($opt_t);
print_log( "process_perfdata.pl-$const{VERSION} starting in BULK Mode called by Nagios", 1 );
$lines = process_perfdata_file();
} elsif ( $opt_b && $opt_n ) {
# Bulk mode with npcd
alarm($opt_t);
print_log( "process_perfdata.pl-$const{VERSION} starting in BULK Mode called by NPCD", 1 );
$lines = process_perfdata_file();
} else {
# Synchronous mode
$opt_t = 5 if $opt_t > 5; # maximum timeout
alarm($opt_t);
print_log( "process_perfdata.pl-$const{VERSION} starting in SYNC Mode", 1 );
%NAGIOS = parse_env();
$lines = process_perfdata();
}
$rt = tv_interval $t0, $t1;
$stats{runtime} = $rt;
$stats{rows} = $lines;
store_internals();
print_log( "PNP exiting (runtime ${rt}s) ...", 1 );
exit 0;
}
#
# Parse %ENV and return a global hash %NAGIOS
#
sub parse_env {
my $job_data = shift;
%NAGIOS = ();
$NAGIOS{DATATYPE} = "SERVICEPERFDATA";
if(defined $opt_gm){
# Gearman Worker
$job_data = decode_base64($job_data);
if($conf{ENCRYPTION} == 1){
$job_data = $cypher->decrypt( $job_data )
}
my @LINE = split(/\t/, $job_data);
foreach my $k (@LINE) {
$k =~ /([A-Z 0-9_]+)::(.*)$/;
$NAGIOS{$1} = $2 if ($2);
}
if ( !$NAGIOS{HOSTNAME} ) {
print_log( "Gearman job data missmatch. Please check your encryption key.", 0 );
return %NAGIOS;
}
} elsif ( defined($opt_b) ){
# Bulk Mode
my @LINE = split(/\t/, $job_data);
foreach my $k (@LINE) {
$k =~ /([A-Z 0-9_]+)::(.*)$/;
$NAGIOS{$1} = $2 if ($2);
}
}else{
if ( ( !$ENV{NAGIOS_HOSTNAME} ) and ( !$ENV{ICINGA_HOSTNAME} ) ) {
print_log( "Cant find Nagios Environment. Exiting ....", 1 );
exit 2;
}
foreach my $key ( sort keys %ENV ) {
if ( $key =~ /^(NAGIOS|ICINGA)_(.*)/ ) {
$NAGIOS{$2} = $ENV{$key};
}
}
}
if ($opt_d) {
$NAGIOS{DATATYPE} = $opt_d;
}
$NAGIOS{DISP_HOSTNAME} = $NAGIOS{HOSTNAME};
$NAGIOS{DISP_SERVICEDESC} = $NAGIOS{SERVICEDESC};
$NAGIOS{HOSTNAME} = cleanup( $NAGIOS{HOSTNAME} );
$NAGIOS{SERVICEDESC} = cleanup( $NAGIOS{SERVICEDESC} );
$NAGIOS{PERFDATA} = $NAGIOS{SERVICEPERFDATA};
$NAGIOS{CHECK_COMMAND} = $NAGIOS{SERVICECHECKCOMMAND};
if ( $NAGIOS{DATATYPE} eq "HOSTPERFDATA" ) {
$NAGIOS{SERVICEDESC} = "_HOST_";
$NAGIOS{DISP_SERVICEDESC} = "Host Perfdata";
$NAGIOS{PERFDATA} = $NAGIOS{HOSTPERFDATA};
$NAGIOS{CHECK_COMMAND} = $NAGIOS{HOSTCHECKCOMMAND};
}
print_log( "Datatype set to '$NAGIOS{DATATYPE}' ", 2 );
return %NAGIOS;
}
#
# Perfdata sanity check
#
sub process_perfdata {
if ( keys( %NAGIOS ) == 1 && defined($opt_gm) ) {
$stats{skipped}++;
return 1;
}
if ( ! defined($NAGIOS{PERFDATA}) && ! defined($opt_gm) ) {
print_log( "No Performance Data for $NAGIOS{HOSTNAME} / $NAGIOS{SERVICEDESC} ", 1 );
if ( !$opt_b ) {
print_log( "PNP exiting ...", 1 );
exit 3;
}
}
if ( $NAGIOS{PERFDATA} =~ /^(.*)\s\[(.*)\]$/ ) {
$NAGIOS{PERFDATA} = $1;
$NAGIOS{CHECK_COMMAND} = $2;
print_log( "Found Perfdata from Distributed Server $NAGIOS{HOSTNAME} / $NAGIOS{SERVICEDESC} ($NAGIOS{PERFDATA})", 1 );
}
else {
print_log( "Found Performance Data for $NAGIOS{HOSTNAME} / $NAGIOS{SERVICEDESC} ($NAGIOS{PERFDATA}) ", 1 );
}
$NAGIOS{PERFDATA} =~ s/,/./g;
$NAGIOS{PERFDATA} =~ s/\s+=/=/g;
$NAGIOS{PERFDATA} =~ s/=\s+/=/g;
$NAGIOS{PERFDATA} .= " ";
parse_perfstring( $NAGIOS{PERFDATA} );
return 1;
}
#
# Process Perfdata in Bulk Mode
#
sub process_perfdata_file {
if ( $opt_b =~ /-PID-(\d+)/ ) {
print_log( "Oops: $opt_b already processed by $1 - please check timeout settings", 0 );
}
print_log( "searching for $opt_b", 2 );
if ( -e "$opt_b" ) {
my $pdfile = "$opt_b" . "-PID-" . $$;
print_log( "renaming $opt_b to $pdfile for bulk update", 2 );
unless ( rename "$opt_b", "$pdfile" ) {
print_log( "ERROR: rename $opt_b to $pdfile failed", 1 );
exit 4;
}
print_log( "reading $pdfile for bulk update", 2 );
open( PDFILE, "< $pdfile" );
my $count = 0;
while (<PDFILE>) {
my $job_data = $_;
$count++;
print_log( "Processing Line $count", 2 );
my @LINE = split(/\t/);
%NAGIOS = (); # cleaning %NAGIOS Hash
#foreach my $k (@LINE) {
# $k =~ /([A-Z 0-9_]+)::(.*)$/;
# $ENV{ 'NAGIOS_' . $1 } = $2 if ($2);
#}
parse_env($job_data);
if ( $NAGIOS{SERVICEPERFDATA} || $NAGIOS{HOSTPERFDATA} ) {
process_perfdata();
} else {
print_log( "No Perfdata. Skipping line $count", 2 );
$stats{skipped}++;
}
}
print_log( "$count lines processed", 1 );
if ( unlink("$pdfile") == 1 ) {
print_log( "$pdfile deleted", 1 );
}else {
print_log( "Could not delete $pdfile:$!", 1 );
}
return $count;
}
else {
print_log( "ERROR: File $opt_b not found", 1 );
}
}
#
# Write Data to RRD Files
#
sub data2rrd {
my @data = @_;
my @rrd_state = ();
my $rrd_storage_type;
print_log( "data2rrd called", 2 );
$NAGIOS{XMLFILE} = $conf{RRDPATH} . "/" . $data[0]{hostname} . "/" . $data[0]{servicedesc} . ".xml";
$NAGIOS{SERVICEDESC} = $data[0]{servicedesc};
$NAGIOS{DISP_SERVICEDESC} = $data[0]{disp_servicedesc};
$NAGIOS{AUTH_SERVICEDESC} = $data[0]{auth_servicedesc} || "";
$NAGIOS{AUTH_HOSTNAME} = $data[0]{auth_hostname} || "";
$NAGIOS{MULTI_PARENT} = "";
$NAGIOS{MULTI_PARENT} = $data[0]{multi_parent} || "";
$TEMPLATE = $data[0]{template};
unless ( -d "$conf{RRDPATH}" ) {
unless ( mkdir "$conf{RRDPATH}" ) {
print_log( "mkdir $conf{RRDPATH}, permission denied ", 1 );
print_log( "PNP exiting ...", 1 );
exit 5;
}
}
unless ( -d "$conf{RRDPATH}/$NAGIOS{HOSTNAME}" ) {
unless ( mkdir "$conf{RRDPATH}/$NAGIOS{HOSTNAME}" ) {
print_log( "mkdir $conf{RRDPATH}/$NAGIOS{HOSTNAME}, permission denied ", 1 );
print_log( "PNP exiting ...", 1 );
exit 6;
}
}
#
# Create PHP Template File
#
open_template( $NAGIOS{XMLFILE} );
@ds_create = ();
$ds_update = '';
for my $i ( 0 .. $#data ) {
print_log( " -- Job $i ", 3 );
my $DS = $i + 1;
#
# for each datasource
#
for my $job ( sort keys %{ $data[$i] } ) {
if ( defined $data[$i]{$job} ) {
print_log( " -- $job -> $data[$i]{$job}", 3 );
}
}
if ( uc($conf{'GLOBAL_RRD_STORAGE_TYPE'}) eq "MULTIPLE" ) {
my $file = $conf{RRDPATH} . "/" . $data[$i]{hostname} . "/" . $data[$i]{servicedesc} . ".rrd";
if ( -e $file ){
print_log("RRD_STORAGE_TYPE=MULTIPLE ignored because $file exists!", 1 ) if $i == 0;
$data[$i]{rrd_storage_type} = "SINGLE";
}
}
if ( $i == 0 ){
$ds_update = "$data[$i]{timet}";
}
if ( $data[$i]{rrd_storage_type} eq "MULTIPLE" ) {
print_log( "DEBUG: MULTIPLE Storage Type", 3 );
$DS = 1;
# PNP 0.4.x Template compatibility
$NAGIOS{RRDFILE} = "";
#
$rrd_storage_type = "MULTIPLE";
$rrdfile = $conf{RRDPATH} . "/" . $data[$i]{hostname} . "/" . $data[$i]{servicedesc} . "_" . $data[$i]{name} . ".rrd";
# DS is set to 1
@ds_create = "DS:$DS:$data[$i]{dstype}:$data[$i]{rrd_heartbeat}:$data[$i]{rrd_min}:$data[$i]{rrd_max}";
$ds_update = "$data[$i]{timet}:$data[$i]{value}";
@rrd_state = write_rrd();
@ds_create = ();
$ds_update = "";
}
else {
print_log( "DEBUG: SINGLE Storage Type", 3 );
# PNP 0.4.x Template compatibility
$NAGIOS{RRDFILE} = $conf{RRDPATH} . "/" . $data[0]{hostname} . "/" . $data[0]{servicedesc} . ".rrd";
#
$rrd_storage_type = "SINGLE";
$rrdfile = $conf{RRDPATH} . "/" . $data[$i]{hostname} . "/" . $data[$i]{servicedesc} . ".rrd";
push( @ds_create, "DS:$DS:$data[$i]{dstype}:$data[$i]{rrd_heartbeat}:$data[$i]{rrd_min}:$data[$i]{rrd_max}" );
$ds_update = "$ds_update:$data[$i]{value}";
}
write_to_template( "TEMPLATE", $data[0]{template} );
write_to_template( "RRDFILE", $rrdfile );
write_to_template( "RRD_STORAGE_TYPE", $data[$i]{rrd_storage_type} );
write_to_template( "RRD_HEARTBEAT", $data[$i]{rrd_heartbeat} );
write_to_template( "IS_MULTI", $data[0]{multi} );
write_to_template( "DS", $DS );
write_to_template( "NAME", $data[$i]{name} );
write_to_template( "LABEL", $data[$i]{label} );
write_to_template( "UNIT", $data[$i]{uom} );
write_to_template( "ACT", $data[$i]{value} );
write_to_template( "WARN", $data[$i]{warning} );
write_to_template( "WARN_MIN", $data[$i]{warning_min} );
write_to_template( "WARN_MAX", $data[$i]{warning_max} );
write_to_template( "WARN_RANGE_TYPE", $data[$i]{warning_range_type} );
write_to_template( "CRIT", $data[$i]{critical} );
write_to_template( "CRIT_MIN", $data[$i]{critical_min} );
write_to_template( "CRIT_MAX", $data[$i]{critical_max} );
write_to_template( "CRIT_RANGE_TYPE", $data[$i]{critical_range_type} );
write_to_template( "MIN", $data[$i]{min} );
write_to_template( "MAX", $data[$i]{max} );
}
if ( $rrd_storage_type eq "SINGLE" ) {
@rrd_state = write_rrd();
}
write_state_to_template(@rrd_state);
write_env_to_template();
close_template( $NAGIOS{XMLFILE} );
}
sub write_rrd {
my @rrd_create = ();
my @rrd_state = ();
print_log( "DEBUG: TPL-> $TEMPLATE", 3 );
print_log( "DEBUG: CRE-> @ds_create", 3 );
print_log( "DEBUG: UPD-> $ds_update", 3 );
if ( !-e "$rrdfile" ) {
@rrd_create = parse_rra_config($TEMPLATE);
if ( $conf{USE_RRDs} == 1 ) {
print_log( "RRDs::create $rrdfile @rrd_create @ds_create --start=$NAGIOS{TIMET} --step=$conf{RRA_STEP}", 2 );
RRDs::create( "$rrdfile", @rrd_create, @ds_create, "--start=$NAGIOS{TIMET}", "--step=$conf{RRA_STEP}" );
my $err = RRDs::error();
if ($err) {
print_log( "RRDs::create $rrdfile @rrd_create @ds_create --start=$NAGIOS{TIMET} --step=$conf{RRA_STEP}", 0 );
print_log( "RRDs::create ERROR $err", 0 );
@rrd_state = ( 1, $err );
$stats{error}++;
}
else {
print_log( "$rrdfile created", 2 );
@rrd_state = ( 0, "just created" );
$stats{create}++;
}
}
else {
print_log( "RRDs Perl Modules are not installed. Falling back to rrdtool system call.", 2 );
print_log( "$conf{RRDTOOL} create $rrdfile @rrd_create @ds_create --start=$NAGIOS{TIMET} --step=$conf{RRA_STEP}", 2 );
system("$conf{RRDTOOL} create $rrdfile @rrd_create @ds_create --start=$NAGIOS{TIMET} --step=$conf{RRA_STEP}");
if ( $? > 0 ) {
print_log( "$conf{RRDTOOL} create $rrdfile @rrd_create @ds_create --start=$NAGIOS{TIMET} --step=$conf{RRA_STEP}", 0 );
print_log( "rrdtool create returns $?", 0 );
@rrd_state = ( $?, "create failed" );
$stats{error}++;
}
else {
print_log( "rrdtool create returns $?", 1 );
@rrd_state = ( 0, "just created" );
$stats{create}++;
}
}
}
else {
if ( $conf{USE_RRDs} == 1 ) {
if ( $conf{RRD_DAEMON_OPTS} ) {
print_log( "RRDs::update --daemon=$conf{RRD_DAEMON_OPTS} $rrdfile $ds_update", 2 );
RRDs::update( "--daemon=$conf{RRD_DAEMON_OPTS}", "$rrdfile", "$ds_update" );
}
else {
print_log( "RRDs::update $rrdfile $ds_update", 2 );
RRDs::update( "$rrdfile", "$ds_update" );
}
my $err = RRDs::error();
if ($err) {
print_log( "RRDs::update $rrdfile $ds_update", 0 );
print_log( "RRDs::update ERROR $err", 0 );
@rrd_state = ( 1, $err );
$stats{error}++;
}
else {
print_log( "$rrdfile updated", 2 );
@rrd_state = ( 0, "successful updated" );
$stats{update}++;
}
}
else {
print_log( "RRDs Perl Modules are not installed. Falling back to rrdtool system call.", 2 );
if ( $conf{RRD_DAEMON_OPTS} ) {
print_log( "$conf{RRDTOOL} update --daemon=$conf{RRD_DAEMON_OPTS} $rrdfile $ds_update", 2 );
system("$conf{RRDTOOL} update --daemon=$conf{RRD_DAEMON_OPTS} $rrdfile $ds_update");
}
else {
print_log( "$conf{RRDTOOL} update $rrdfile $ds_update", 2 );
system("$conf{RRDTOOL} update $rrdfile $ds_update");
}
if ( $? > 0 ) {
print_log( "$conf{RRDTOOL} update $rrdfile $ds_update", 0 );
print_log( "rrdtool update returns $?", 0 );
@rrd_state = ( $?, "update failed" );
$stats{error}++;
}
else {
print_log( "rrdtool update returns $?", 1 );
@rrd_state = ( $?, "successful updated" );
$stats{update}++;
}
}
}
return @rrd_state;
}
#
# Write Template
#
sub open_template {
my $xmlfile = shift;
$delayed_write = 0;
if( -e $xmlfile ){
my $mtime = (stat($xmlfile))[9];
my $t = time();
my $age = ($t - $mtime);
if ( $age < $conf{'XML_UPDATE_DELAY'} ){
print_log( "DEBUG: XML File is $age seconds old. No update needed", 3 );
$delayed_write = 1;
return;
}
print_log( "DEBUG: XML File is $age seconds old. UPDATE!", 3 );
}
open( XML, "> $xmlfile.$$" ) or die "Cant create temporary XML file ", $!;
print XML "<?xml version=\"1.0\" encoding=\"" . $conf{XML_ENC} . "\" standalone=\"yes\"?>\n";
print XML "<NAGIOS>\n";
}
#
# Close Template FH
#
sub close_template {
return if $delayed_write == 1;
my $xmlfile = shift;
printf( XML " <XML>\n" );
printf( XML " <VERSION>%d</VERSION>\n", $const{'XML_STRUCTURE_VERSION'} );
printf( XML " </XML>\n" );
printf( XML "</NAGIOS>\n" );
close(XML);
rename( "$xmlfile.$$", "$xmlfile" );
}
#
# Add Lines
#
sub write_to_template {
return if $delayed_write == 1;
my $tag = shift;
my $data = shift;
if ( !defined $data ) {
$data = "";
}
if ( $tag =~ /^TEMPLATE$/ ) {
printf( XML " <DATASOURCE>\n" );
printf( XML " <%s>%s</%s>\n", $tag, "$data", $tag );
}
elsif ( $tag =~ /^MAX$/ ) {
printf( XML " <%s>%s</%s>\n", $tag, "$data", $tag );
printf( XML " </DATASOURCE>\n" );
}
else {
printf( XML " <%s>%s</%s>\n", $tag, "$data", $tag );
}
}
sub write_state_to_template {
return if $delayed_write == 1;
my @rrd_state = @_;
printf( XML " <RRD>\n" );
printf( XML " <RC>%s</RC>\n", $rrd_state[0] );
printf( XML " <TXT>%s</TXT>\n", $rrd_state[1] );
printf( XML " </RRD>\n" );
}
#
# Store the complete Nagios ENV
#
sub write_env_to_template {
return if $delayed_write == 1;
foreach my $key ( sort keys %NAGIOS ) {
$NAGIOS{$key} = urlencode($NAGIOS{$key});
printf( XML " <NAGIOS_%s>%s</NAGIOS_%s>\n", $key, $NAGIOS{$key}, $key );
}
}
#
# Recursive Template search
#
sub adjust_template {
my $command = shift;
my $uom = shift;
my $count = shift;
my @temp_template = split /\!/, $command;
my $initial_template = cleanup( $temp_template[0] );
my $template = cleanup( $temp_template[0] );
my %CTPL = read_custom_template ( $command, $uom, $count );
#
if ( $CTPL{'TEMPLATE'} ne $initial_template ){
%CTPL = read_custom_template ( $CTPL{'TEMPLATE'}, $uom, $count );
}
return %CTPL;
}
#
# Analyse check_command to find PNP Template .
#
sub read_custom_template {
my $command = shift;
my $uom = shift;
my $count = shift;
my @dstype_list = ();
my $use_min_on_create = 0;
my $use_max_on_create = 0;
my $rrd_storage_type = $conf{'RRD_STORAGE_TYPE'};
my $rrd_heartbeat = $conf{'RRD_HEARTBEAT'};
if ( defined($conf{'UOM2TYPE'}{$uom}) ) {
$dstype = $conf{'UOM2TYPE'}{$uom};
print_log( "DEBUG: DSTYPE adjusted to $dstype by UOM", 3 );
}else {
$dstype = 'GAUGE';
}
print_log( "DEBUG: RAW Command -> $command", 3 );
my @temp_template = split /\!/, $command;
my $template = cleanup( $temp_template[0] );
$template = trim($template);
my $template_cfg = "$conf{CFG_DIR}/check_commands/$template.cfg";
if ( -e $template_cfg ) {
print_log( "DEBUG: adjust_template() => $template_cfg", 3 );
my $initial_dstype = $dstype;
open FH, "<", $template_cfg;
while (<FH>) {
next if /^#/;
next if /^$/;
s/#.*//;
s/ //g;
if (/^(.*)=(.*)$/) {
if ( $1 eq "DATATYPE" ) {
$dstype = uc($2);
$dstype =~ s/ //g;
@dstype_list = split /,/, $dstype;
if ( exists $dstype_list[$count] && $dstype_list[$count] =~ /^(COUNTER|GAUGE|ABSOLUTE|DERIVE)$/ ) {
$dstype = $dstype_list[$count];
print_log( "Adapting RRD Datatype to \"$dstype\" as defined in $template_cfg with key $count", 2 );
}
elsif ( $dstype =~ /^(COUNTER|GAUGE|ABSOLUTE|DERIVE)$/ ) {
print_log( "Adapting RRD Datatype to \"$dstype\" as defined in $template_cfg", 2 );
}
else {
print_log( "RRD Datatype \"$dstype\" defined in $template_cfg is invalid", 2 );
$dstype = $initial_dstype;
}
}
if ( $1 eq "CUSTOM_TEMPLATE" ) {
print_log( "Adapting Template using ARG $2", 2 );
my $i = 1;
my @keys = split /,/, $2;
foreach my $keys (@keys) {
if ( $i == 1 && exists $temp_template[$keys] ) {
$template = trim( $temp_template[$keys] );
print_log( "Adapting Template to $template.php (added ARG$keys)", 2 );
}elsif( exists $temp_template[$keys] ){
$template .= "_" . trim( $temp_template[$keys] );
print_log( "Adapting Template to $template.php (added ARG$keys)", 2 );
}
$i++;
}
print_log( "Adapting Template to $template.php as defined in $template_cfg", 2 );
}
if ( $1 eq "USE_MIN_ON_CREATE" && $2 eq "1" ) {
$use_min_on_create = 1;
}
if ( $1 eq "USE_MAX_ON_CREATE" && $2 eq "1" ) {
$use_max_on_create = 1;
}
if ( $1 eq "RRD_STORAGE_TYPE" && uc($2) eq "MULTIPLE" ) {
$rrd_storage_type = uc($2);
}
if ( $1 eq "RRD_HEARTBEAT" ) {
$rrd_heartbeat = $2;
}
}
}
close FH;
}
else {
print_log( "No Custom Template found for $template ($template_cfg) ", 2 );
print_log( "RRD Datatype is $dstype", 3 );
}
print_log( "Template is $template.php", 2 );
my %CTPL = (
TEMPLATE => $template,
DSTYPE => $dstype,
RRD_STORAGE_TYPE => $rrd_storage_type,
RRD_HEARTBEAT => $rrd_heartbeat,
USE_MIN_ON_CREATE => $use_min_on_create,
USE_MAX_ON_CREATE => $use_max_on_create,
);
return %CTPL;
}
#
# Parse process_perfdata.cfg
#
sub parse_config {
my $config_file = shift;
my $line = 0;
if ( -e $config_file ) {
open CFG, '<', "$config_file";
while (<CFG>) {
$line++;
chomp;
s/ //g;
next if /^#/;
next if /^$/;
s/#.*//;
if (/^(.*)=(.*)$/) {
if ( defined $conf{$1} ) {
$conf{$1} = $2;
}
}
}
close CFG;
print_log( "Using Config File $config_file parameters", 2 );
}
else {
print_log( "Config File $config_file not found, using defaults", 2 );
}
}
#
# Parse rra.cfg
#
sub parse_rra_config {
my $template = shift;
my $rra_template = "";
my @rrd_create = @default_rrd_create;
if ( -e $conf{'CFG_DIR'} . "/" . $template . ".rra.cfg" ) {
$rra_template = $conf{'CFG_DIR'} . "/" . $template . ".rra.cfg";
print_log( "Reading $rra_template", 2 );
}
elsif ( -e $conf{'RRA_CFG'} ) {
$rra_template = $conf{'RRA_CFG'};
print_log( "Reading $conf{'RRA_CFG'}", 2 );
}
else {
print_log( "No rra.cfg found. Using default values.", 2 );
}
if ( $rra_template ne "" ) {
@rrd_create = ();
open RRA, "<", $rra_template;
while (<RRA>) {
next if /^#/;
next if /^$/;
s/#.*//;
if(/^RRA_STEP=(\d+)/i){
$conf{'RRA_STEP'} = $1;
next;
}
chomp;
push @rrd_create, "$_";
}
close RRA;
}
else {
@rrd_create = @default_rrd_create;
}
return @rrd_create;
}
#
# Function adapted from Nagios::Plugin::Performance
# Thanks to Gavin Carr and Ton Voon
#
sub _parse {
# Nagios::Plugin::Performance
my $string = shift;
my $tmp_string = $string;
$string =~ s/^([^=]+)=([\d\.\-]+)([\w\/%]*);?([\d\.\-:~@]+)?;?([\d\.\-:~@]+)?;?([\d\.\-]+)?;?([\d\.\-]+)?;?\s*//;
if ( $tmp_string eq $string ) {
print_log( "No pattern match in function _parse($string)", 2 );
return undef;
}
return undef unless ( ( defined $1 && $1 ne "" ) && ( defined $2 && $2 ne "" ) );
# create hash from all performance data values
my %p = (
"label" => $1,
"name" => $1,
"value" => $2,
"uom" => $3,
"warning" => $4,
"critical" => $5,
"min" => $6,
"max" => $7
);
$p{label} =~ s/[']//g; # cleanup
$p{name} =~ s/[']//g; # cleanup
$p{name} =~ s/[\/\\]/_/g; # cleanup
$p{name} = cleanup($p{name});
if ( $p{uom} eq "%" ) {
$p{uom} = "%%";
print_log( "DEBUG: UOM adjust = $p{uom}", 3 );
}
#
# Check for warning and critical ranges
#
if ( $p{warning} && $p{warning} =~ /^([\d\.\-~@]+)?:([\d\.\-~@]+)?$/ ) {
print_log( "DEBUG: Processing warning ranges ( $p{warning} )", 3 );
$p{warning_min} = $1;
$p{warning_max} = $2;
delete( $p{warning} );
if ( $p{warning_min} =~ /^@/ ) {
$p{warning_min} =~ s/@//;
$p{warning_range_type} = "inside";
}
else {
$p{warning_range_type} = "outside";
}
}
if ( $p{critical} && $p{critical} =~ /^([\d\.\-~@]+)?:([\d\.\-~@]+)?$/ ) {
print_log( "DEBUG: Processing critical ranges ( $p{critical} )", 3 );
$p{critical_min} = $1;
$p{critical_max} = $2;
delete( $p{critical} );
if ( $p{critical_min} =~ /^@/ ) {
$p{critical_min} =~ s/@//;
$p{critical_range_type} = "inside";
}
else {
$p{critical_range_type} = "outside";
}
}
# Strip Range indicators
$p{warning} =~ s/[~@]// if($p{warning});
$p{critical} =~ s/[~@]// if($p{critical});
return ( $string, %p );
}
#
# clean Strings
#
sub cleanup {
my $string = shift;
if ($string) {
$string =~ s/[ :\/\\]/_/g;
}
return $string;
}
#
# Urlencode
#
sub urlencode {
my $string = shift;
if ($string) {
$string =~ s/([<>&])/sprintf("%%%02x",ord($1))/eg; # URLencode;
}
return $string;
}
#
# Trim leading whitespaces
#
sub trim {
my $string = shift;
$string =~ s/^\s*//g;
return $string;
}
#
# Parse the Performance String and call data2rrd()
#
sub parse_perfstring {
#
# Default RRD Datatype
# Value will be overwritten by adjust_template()
#
my %CTPL = ();
$dstype = "GAUGE";
my $perfstring = shift;
my $is_multi = "0";
my @perfs;
my @multi;
my %p;
my $use_min_on_create = 0;
my $use_max_on_create = 0;
#
# check_multi
#
if ( $perfstring =~ /^[']?([a-zA-Z0-9\.\-_\s\/\#]+)::([a-zA-Z0-9\.\-_\s]+)::([^=]+)[']?=/ ) {
$is_multi = 1;
print_log( "check_multi Perfdata start", 3 );
my $count = 0;
my $check_multi_blockcount = 0;
my $multi_parent = cleanup( $NAGIOS{SERVICEDESC} );
my $auth_servicedesc = $NAGIOS{DISP_SERVICEDESC};
while ($perfstring) {
( $perfstring, %p ) = _parse($perfstring);
if ( !$p{label} ) {
print_log( "Invalid Perfdata detected ", 1 );
$stats{invalid}++;
@perfs = ();
last;
}
if ( $p{label} =~ /^[']?([a-zA-Z0-9\.\-_\s\/\#]+)::([a-zA-Z0-9\.\-_\s]+)::([^=]+)[']?$/ ) {
@multi = ( $1, $2, $3 );
if ( $count == 0 ) {
print_log( "DEBUG: First check_multi block", 3 );
# Keep servicedesc while processing the first block.
$p{servicedesc} = cleanup( $NAGIOS{SERVICEDESC} );
$p{disp_servicedesc} = $NAGIOS{DISP_SERVICEDESC};
$p{auth_servicedesc} = $auth_servicedesc;
$p{multi} = 1;
$p{multi_parent} = $multi_parent;
}
else {
print_log( "DEBUG: A new check_multi block ($count) starts", 3 );
$p{servicedesc} = cleanup( $multi[0] ); # Use the multi servicedesc.
$p{multi} = 2;
$p{multi_parent} = $multi_parent;
$p{servicedesc} = cleanup( $multi[0] ); # Use the multi servicedesc.
$p{disp_servicedesc} = cleanup( $multi[0] ); # Use the multi servicedesc.
$p{auth_servicedesc} = $auth_servicedesc;
data2rrd(@perfs) if ( $#perfs >= 0 ); # Process when a new block starts.
@perfs = (); # Clear the perfs array.
# reset check_multi block count
$check_multi_blockcount = 0;
}
%CTPL = adjust_template( $multi[1], $p{uom}, $check_multi_blockcount++ );
if ( $CTPL{'USE_MAX_ON_CREATE'} == 1 && defined $p{max} ) {
$p{rrd_max} = $p{max};
} else {
$p{rrd_max} = "U";
}
if ( $CTPL{'USE_MIN_ON_CREATE'} == 1 && defined $p{min} ) {
$p{rrd_min} = $p{min};
} elsif( $CTPL{'DSTYPE'} eq 'DERIVE' ){
$p{rrd_min} = 0; # Add minimum value 0 if DSTYPE = DERIVE
} else {
$p{rrd_min} = "U";
}
$p{template} = $CTPL{'TEMPLATE'};
$p{dstype} = $CTPL{'DSTYPE'};
$p{rrd_storage_type} = $CTPL{'RRD_STORAGE_TYPE'};
$p{rrd_heartbeat} = $CTPL{'RRD_HEARTBEAT'};
$p{label} = cleanup( $multi[2] ); # store the original label from check_multi header
$p{name} = cleanup( $multi[2] ); # store the original label from check_multi header
$p{hostname} = cleanup( $NAGIOS{HOSTNAME} );
$p{disp_hostname} = $NAGIOS{DISP_HOSTNAME};
$p{auth_hostname} = $NAGIOS{HOSTNAME};
$p{timet} = $NAGIOS{TIMET};
push @perfs, {%p};
$count++;
}
else {
print_log( "DEBUG: Next check_multi data for block $count multiblock $check_multi_blockcount", 3 );
# additional check_multi data
%CTPL = adjust_template( $multi[1], $p{uom}, $check_multi_blockcount++ );
if ( $CTPL{'USE_MAX_ON_CREATE'} == 1 && defined $p{max} ) {
$p{rrd_max} = $p{max};
} else {
$p{rrd_max} = "U";
}
if ( $CTPL{'USE_MIN_ON_CREATE'} == 1 && defined $p{min} ) {
$p{rrd_min} = $p{min};
} elsif( $CTPL{'DSTYPE'} eq 'DERIVE' ){
$p{rrd_min} = 0; # Add minimum value 0 if DSTYPE = DERIVE
} else {
$p{rrd_min} = "U";
}
$p{template} = $CTPL{'TEMPLATE'};
$p{dstype} = $CTPL{'DSTYPE'};
$p{rrd_storage_type} = $CTPL{'RRD_STORAGE_TYPE'};
$p{rrd_heartbeat} = $CTPL{'RRD_HEARTBEAT'};
$p{hostname} = cleanup( $NAGIOS{HOSTNAME} );
$p{disp_hostname} = $NAGIOS{DISP_HOSTNAME};
$p{auth_hostname} = $NAGIOS{HOSTNAME};
$p{timet} = $NAGIOS{TIMET};
if ( $count == 1 ) {
$p{servicedesc} = cleanup( $NAGIOS{SERVICEDESC} ); # Use the servicedesc.
$p{disp_servicedesc} = $NAGIOS{DISP_SERVICEDESC}; # Use the servicedesc.
} else {
$p{servicedesc} = cleanup( $multi[0] ); # Use the multi servicedesc.
$p{disp_servicedesc} = $multi[0]; # Use the multi servicedesc.
}
$p{multi} = $is_multi;
$p{multi_parent} = $multi_parent;
$p{auth_servicedesc} = $auth_servicedesc; # Use the servicedesc.
push @perfs, {%p};
}
}
data2rrd(@perfs) if ( $#perfs >= 0 );
@perfs = ();
} else {
#
# Normal Performance Data
#
print_log( "DEBUG: Normal perfdata", 3 );
my $count = 0;
while ($perfstring) {
( $perfstring, %p ) = _parse($perfstring);
if ( !$p{label} ) {
print_log( "Invalid Perfdata detected ", 1 );
@perfs = ();
last;
}
%CTPL = adjust_template( $NAGIOS{CHECK_COMMAND}, $p{uom}, $count );
if ( $CTPL{'USE_MAX_ON_CREATE'} == 1 && defined $p{max} ) {
$p{rrd_max} = $p{max};
} else {
$p{rrd_max} = "U";
}
if ( $CTPL{'USE_MIN_ON_CREATE'} == 1 && defined $p{min} ) {
$p{rrd_min} = $p{min};
} elsif ( $CTPL{'DSTYPE'} eq 'DERIVE' ){
$p{rrd_min} = 0; # Add minimum value 0 if DSTYPE = DERIVE
} else {
$p{rrd_min} = "U";
}
$p{template} = $CTPL{'TEMPLATE'};
$p{dstype} = $CTPL{'DSTYPE'};
$p{rrd_storage_type} = $CTPL{'RRD_STORAGE_TYPE'};
$p{rrd_heartbeat} = $CTPL{'RRD_HEARTBEAT'};
$p{multi} = $is_multi;
$p{hostname} = cleanup( $NAGIOS{HOSTNAME} );
$p{disp_hostname} = $NAGIOS{DISP_HOSTNAME};
$p{auth_hostname} = $NAGIOS{DISP_HOSTNAME};
$p{servicedesc} = cleanup( $NAGIOS{SERVICEDESC} );
$p{disp_servicedesc} = $NAGIOS{DISP_SERVICEDESC};
$p{auth_servicedesc} = $NAGIOS{DISP_SERVICEDESC};
$p{timet} = $NAGIOS{TIMET};
push @perfs, {%p};
$count++;
}
data2rrd(@perfs) if ( $#perfs >= 0 );
@perfs = ();
}
}
#
# Write to Logfile
#
sub print_log {
my $out = shift;
my $severity = shift;
if ( $severity <= $conf{LOG_LEVEL} ) {
open( LOG, ">>" . $conf{LOG_FILE} ) || die "Can't open logfile ($conf{LOG_FILE}) ", $!;
if ( -s LOG > $conf{LOG_FILE_MAX_SIZE} ) {
truncate( LOG, 0 );
printf( LOG "File truncated" );
}
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
printf( LOG "%02d-%02d-%02d %02d:%02d:%02d [%d] [%d] %s\n", $year + 1900, $mon + 1, $mday, $hour, $min, $sec, $$, $severity, $out );
close(LOG);
}
}
#
# Signals and Handlers
#
sub init_signals {
$SIG{'INT'} = \&handle_signal;
$SIG{'QUIT'} = \&handle_signal;
$SIG{'ALRM'} = \&handle_signal;
$SIG{'ILL'} = \&handle_signal;
$SIG{'ABRT'} = \&handle_signal;
$SIG{'FPE'} = \&handle_signal;
$SIG{'SEGV'} = \&handle_signal;
$SIG{'TERM'} = \&handle_signal;
$SIG{'BUS'} = \&handle_signal;
$SIG{'SYS'} = \&handle_signal;
$SIG{'XCPU'} = \&handle_signal;
$SIG{'XFSZ'} = \&handle_signal;
$SIG{'IOT'} = \&handle_signal;
$SIG{'PIPE'} = \&handle_signal;
$SIG{'HUP'} = \&handle_signal;
$SIG{'CHLD'} = \&handle_signal;
}
#
# Handle Signals
#
sub handle_signal {
my ($signal) = (@_);
#
# Gearman child process
#
if ( defined ( $opt_gm ) ){
if($signal eq "CHLD" && defined($opt_gm) ){
my $pid = waitpid(-1, &WNOHANG);
if($pid == -1){
print_log( "### no hanging child ###", 1 );
} elsif ( WIFEXITED($?)) {
print_log( "### child $pid exited ###", 1 );
$children--;
} else {
print_log( "### wrong signal ###", 1 );
$children--;
}
$SIG{'CHLD'} = \&handle_signal;
}
if($signal eq "INT" || $signal eq "TERM"){
local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
kill $signal => keys %children;
print_log( "*** process_perfdata.pl terminated on signal $signal", 0 );
pidlock("remove");
exit; # clean up with dignity
}
print_log( "*** process_perfdata.pl received signal $signal (ignored)", 0 );
}else{
if ( $signal eq "ALRM" ) {
print_log( "*** TIMEOUT: Timeout after $opt_t secs. ***", 0 );
if ( $opt_b && !$opt_n ) {
print_log( "*** TIMEOUT: Deleting current file to avoid loops", 0 );
print_log( "*** TIMEOUT: Please check your process_perfdata.cfg", 0 );
}
elsif ( $opt_b && $opt_n ) {
print_log( "*** TIMEOUT: Deleting current file to avoid NPCD loops", 0 );
print_log( "*** TIMEOUT: Please check your process_perfdata.cfg", 0 );
}
if ($opt_b) {
my $pdfile = "$opt_b" . "-PID-" . $$;
if ( unlink("$pdfile") == 1 ) {
print_log( "*** TIMEOUT: $pdfile deleted", 0 );
}
else {
print_log( "*** TIMEOUT: Could not delete $pdfile:$!", 0 );
}
}
my $temp_file = "$conf{RRDPATH}/$NAGIOS{HOSTNAME}/$NAGIOS{SERVICEDESC}.xml.$$";
if ( -f $temp_file ) {
unlink($temp_file);
}
$t1 = [gettimeofday];
$rt = tv_interval $t0, $t1;
$stats{runtime} = $rt;
print_log( "*** Timeout while processing Host: \"$NAGIOS{HOSTNAME}\" Service: \"$NAGIOS{SERVICEDESC}\"", 0 );
print_log( "*** process_perfdata.pl terminated on signal $signal", 0 );
exit 7;
}
}
}
sub init_stats {
%stats = (
timet => time,
error => 0,
invalid => 0,
skipped => 0,
runtime => 0,
rows => 0,
create => 0,
update => 0,
);
}
#
# Store some internal runtime infos
#
sub store_internals {
if( ! -w $conf{'STATS_DIR'}){
print_log("*** ERROR: ".$conf{'STATS_DIR'}." is not writable or does not exist",0);
return;
}
my $statsfile = $conf{'STATS_DIR'}."/".(int $stats{timet} / 60);
open( STAT, ">> $statsfile" ) or die "Cant create statistic file ", $!;
printf(STAT "%d %f %d %d %d %d %d %d\n", $stats{timet},$stats{runtime},$stats{rows},$stats{update},$stats{create},$stats{error},$stats{invalid},$stats{skipped});
close(STAT);
check_internals();
}
#
# Search for statistic files
#
sub check_internals {
my $file;
my @files;
opendir(STATS, $conf{'STATS_DIR'});
while ( defined ( my $file = readdir STATS) ){
next if $file =~ /^\.\.?$/; # skip . and ..
next if $file =~ /-PID-/; # skip temporary files
next if $file == (int $stats{timet} / 60); # skip our current file
push @files, $file;
}
read_internals(@files);
}
#
# Read and aggregate files found by check_internals()
#
sub read_internals {
my @files = @_;
my @chunks;
foreach my $file (sort { $a <=> $b} @files){
unless ( rename($conf{'STATS_DIR'}."/".$file, $conf{'STATS_DIR'}."/".$file."-PID-".$$) ){
print_log( "ERROR: renaming stats file " . $conf{'STATS_DIR'}."/".$file . " to " . $conf{'STATS_DIR'}."/".$file."-PID-".$$ . " failed", 1 );
next;
}
open( STAT, "< ".$conf{'STATS_DIR'}."/".$file."-PID-".$$ );
%stats = (
timet => 0,
error => 0,
invalid => 0,
skipped => 0,
runtime => 0,
rows => 0,
create => 0,
update => 0,
);
while(<STAT>){
@chunks = split();
$stats{timet} = $chunks[0];
$stats{runtime} += $chunks[1];
$stats{rows} += $chunks[2];
$stats{update} += $chunks[3];
$stats{create} += $chunks[4];
$stats{error} += $chunks[5];
$stats{invalid} += $chunks[6];
$stats{skipped} += $chunks[7];
}
close(STAT);
unlink($conf{'STATS_DIR'}."/".$file."-PID-".$$);
process_internals();
}
}
#
#
#
sub process_internals {
my $last_rrd_dtorage_type = $conf{'RRD_STORAGE_TYPE'};
$conf{'RRD_STORAGE_TYPE'} = "MULTIPLE";
%NAGIOS = (
HOSTNAME => '.pnp-internal',
DISP_HOSTNAME => 'pnp-internal',
SERVICEDESC => 'runtime',
DISP_SERVICEDESC => 'runtime',
TIMET => $stats{timet},
DATATYPE => 'SERVICEPERFDATA',
CHECK_COMMAND => 'pnp-runtime',
PERFDATA => "runtime=".$stats{runtime}."s rows=".$stats{rows}." errors=".$stats{error}." invalid=".$stats{invalid}." skipped=".$stats{skipped} ." update=".$stats{update}. " create=".$stats{create}
);
parse_perfstring( $NAGIOS{PERFDATA} );
$conf{'RRD_STORAGE_TYPE'} = $last_rrd_dtorage_type;
}
#
# Gearman Worker Daemon
#
sub daemonize {
if( defined($opt_daemon) ){
print_log("daemonize init",1);
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
defined( my $pid = fork ) or die "Can't fork: $!";
exit if $pid;
pidlock("create");
setsid or die "Can't start a new session: $!";
} else {
pidlock("create");
}
# Fork off our children.
for (1 .. $conf{'PREFORK'}) {
new_child();
print_log( "starting child process $children", 1 );
}
while (1) {
sleep; # wait for a signal (i.e., child's death)
for (my $i = $children; $i < $conf{'PREFORK'}; $i++) {
print_log("starting new child (running = $i)",1);
new_child(); # top up the child pool
}
}
return;
}
#
# start a new worker process
#
sub new_child {
my $pid;
my $sigset;
my $req = 0;
# block signal for fork
$sigset = POSIX::SigSet->new(SIGINT);
sigprocmask(SIG_BLOCK, $sigset)
or die "Can't block SIGINT for fork: $!\n";
die "fork: $!" unless defined ($pid = fork);
if ($pid) {
# Parent records the child's birth and returns.
sigprocmask(SIG_UNBLOCK, $sigset)
or die "Can't unblock SIGINT for fork: $!\n";
$children{$pid} = 1;
$children++;
return;
} else {
# Child can *not* return from this subroutine.
$SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
# unblock signals
sigprocmask(SIG_UNBLOCK, $sigset)
or die "Can't unblock SIGINT for fork: $!\n";
my $worker = Gearman::Worker->new();
$worker->job_servers($conf{'GEARMAN_HOST'});
$worker->register_function("perfdata", 2, sub { return main(@_); });
my %opt = (
on_complete => sub { $req++; },
stop_if => sub { if ( $req > $conf{'REQUESTS_PER_CHILD'} ) { return 1;}; }
);
print_log("connecting to gearmand '".$conf{'GEARMAN_HOST'}."'",0);
$worker->work( %opt );
print_log("max requests per child reached (".$conf{'REQUESTS_PER_CHILD'}.")",1);
# this exit is VERY important, otherwise the child will become
# a producer of more and more children, forking yourself into
# process death.
exit;
}
}
#
# Create a pid file
#
sub pidlock {
return unless defined $opt_pidfile;
my $action = shift;
my $PIDFILE = $opt_pidfile;
if($action eq "create"){
if ( -e $PIDFILE ) {
if ( open( OLDPID, "<$PIDFILE" ) ) {
$_ = <OLDPID>;
chop($_);
my $oldpid = $_;
close(OLDPID);
if ( -e "/proc/$oldpid/cmdline" ) {
print_log("Another instance is already running with PID: $oldpid",0);
exit 0;
} else {
print_log("Pidfile $PIDFILE seems to be stale!",0);
print_log("Removing old pidfile",0);
unlink $PIDFILE;
}
}
}
if ( !open( PID, ">$PIDFILE" ) ) {
print_log("Can not create $PIDFILE ( $! )",0);
exit 1;
}
print( PID "$$\n" );
close(PID);
print_log("Pidfile ($PIDFILE) created",0);
}elsif( $action eq "remove" ){
if ( -e $PIDFILE ) {
print_log("Removing pidfile ($PIDFILE)",0);
unlink $PIDFILE;
}
}
}
#
# Read crypt key
#
sub read_keyfile {
my $file = shift;
my $key = '';
if( -r $file){
open(FH, "<", $file);
while(<FH>){
chomp(); # avoid \n on last field
$conf{'KEY'} = $_;
last;
}
close(FH);
print_log("Using encryption key specified in '$file'",0);
return 1;
}else{
print_log("Using encryption key specified in ".$conf{'CFG_DIR'}."/process_perfdata.cfg",0);
return 0;
}
}
#
#
#
sub print_help {
print <<EOD;
Copyright (c) 2005-2010 Joerg Linge <pitchfork\@pnp4nagios.org>
Use process_perfdata.pl to store Nagios Plugin Performance Data into RRD Databases
Options:
-h, --help
Print detailed help screen
-V, --version
Print version information
-t, --timeout=INTEGER
Seconds before process_perfdata.pl times out (default: $opt_t)
-i, --inetd
Use this Option if process_perfdata.pl is executed by inetd/xinetd.
-d, --datatype
Defaults to \"SERVICEPERFDATA\". Use \"HOSTPERFDATA\" to process Perfdata from regular Host Checks
Only used in default or inetd Mode
-b, --bulk
Provide a file for bulk update
-c, --config
Optional process_perfdata config file
Default: /etc/pnp4nagios/process_perfdata.cfg
Gearman Worker Options:
--gearman
Start in gearman worker mode
--daemon
Run as daemon
--pidfile=/var/run/process_perfdata.pid
The pidfile used while running in as gearman worker daemon
EOD
exit 0;
}
#
#
#
sub print_version {
print "Version: process_perfdata.pl $const{VERSION}\n";
print "Copyright (c) 2005-2010 Joerg Linge <pitchfork\@pnp4nagios.org>\n";
exit 0;
}
|