trap_restart.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use warnings;
  7. use Encode;
  8. use open qw(:std :encoding(UTF-8));
  9. no warnings 'utf8';
  10. use FindBin '$Bin';
  11. use lib "/opt/Eye/scripts";
  12. use strict;
  13. use Time::Local;
  14. use FileHandle;
  15. use Data::Dumper;
  16. use eyelib::config;
  17. use eyelib::main;
  18. use eyelib::database;
  19. use eyelib::net_utils;
  20. use DBI;
  21. $debug=0;
  22. $log_enable = 0;
  23. my $host = <STDIN>; # Read the Hostname - First line of input from STDIN
  24. chomp($host);
  25. my $ip = <STDIN>; # Read the IP - Second line of input
  26. chomp($ip);
  27. my %oids;
  28. my @lines=();
  29. while(<STDIN>) {
  30. chomp($_);
  31. push(@lines,$_);
  32. }
  33. ########## write log #############
  34. my ($host_ip) = $oids{'SNMP-COMMUNITY-MIB::snmpTrapAddress.0'} || ($ip =~ m#^UDP\:\s+\[([\d\.]+)\]\:\d+.*$#o);
  35. my $TRAP_DIR="/var/log/trapd/$host_ip";
  36. if (! -e $TRAP_DIR) { mkdir $TRAP_DIR, 0755; }
  37. my $TRAP_FILE = $TRAP_DIR. "/trap.log";
  38. my ($sec,$min,$hour,$mday,$mon,$year) = (localtime())[0,1,2,3,4,5];
  39. $mon += 1; $year += 1900;
  40. my $date = sprintf "%04d%02d%02d-%02d%02d%02d",$year,$mon,$mday,$hour,$min,$sec;
  41. open(TRAPFILE, ">> $TRAP_FILE");
  42. my $coldstart=0;
  43. foreach my $trap_lines (@lines) {
  44. print(TRAPFILE "$date [".$$."] LINE: $trap_lines\n");
  45. my $key;
  46. my $value;
  47. if ($trap_lines=~/^(\S+)\s+(.*)/) {
  48. $key = $1;
  49. $value = $2;
  50. }
  51. next if (!$key);
  52. $value='' if (!$value);
  53. $value=~s/\"//g;
  54. $oids{$key}=$value;
  55. }
  56. foreach my $key (keys %oids) {
  57. print(TRAPFILE "$date [".$$."] IP: $host_ip HOST: $host TRAP: $key VALUE: $oids{$key}\n");
  58. if ($oids{$key}=~/coldstart/i) { $coldstart=1; }
  59. }
  60. exit if (!$coldstart);
  61. ############ find device ###############
  62. my $IP_ATON=StrToIp($host_ip);
  63. print(TRAPFILE "$date [".$$."] search host by ip [$host_ip] aton: $IP_ATON\n");
  64. #get device
  65. my $sSQL="SELECT dns_name FROM user_auth where deleted=0 and ip_int='".$IP_ATON."'";
  66. my $ret=get_custom_record($dbh,$sSQL);
  67. my $device_name=$ret->{dns_name};
  68. print(TRAPFILE "$date [".$$."] name: $device_name\n");
  69. exit if (!$device_name);
  70. ############ notify nagios #############
  71. my $svc_description = 'Uptime';
  72. my $stringoutput = 'WARN: Trap - Restart device! '.$svc_description;
  73. my $retcode = 1;
  74. my $run_cmd = "/etc/nagios4/scripts/eventhandlers/submit_check_result '".$device_name."' '".$svc_description."' $retcode '".$stringoutput."'";
  75. print(TRAPFILE "$date [".$$."] run: $run_cmd\n");
  76. system($run_cmd);
  77. close(TRAPFILE);
  78. exit;