1
0

syslog-stat.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use Encode;
  8. no warnings 'utf8';
  9. use English;
  10. use base;
  11. use FindBin '$Bin';
  12. use lib "/opt/Eye/scripts";
  13. use strict;
  14. use Time::Local;
  15. use FileHandle;
  16. use eyelib::config;
  17. use eyelib::main;
  18. use eyelib::database;
  19. use Data::Dumper;
  20. use DBI;
  21. use Time::Local;
  22. use Date::Parse;
  23. use Getopt::Long;
  24. use IO::Socket::UNIX qw( SOCK_STREAM );
  25. use Proc::Daemon;
  26. use Cwd;
  27. my $pf = '/run/eye/syslog-stat.pid';
  28. my $socket_path='/run/syslog-ng.socket';
  29. my $daemon = Proc::Daemon->new(
  30. pid_file => $pf,
  31. work_dir => $HOME_DIR
  32. );
  33. # are you running? Returns 0 if not.
  34. my $pid = $daemon->Status($pf);
  35. my $daemonize = 1;
  36. GetOptions(
  37. 'daemon!' => \$daemonize,
  38. "help" => \&usage,
  39. "reload" => \&reload,
  40. "restart" => \&restart,
  41. "start" => \&run,
  42. "status" => \&status,
  43. "stop" => \&stop
  44. ) or &usage;
  45. exit(0);
  46. sub stop {
  47. if ($pid) {
  48. print "Stopping pid $pid...";
  49. if ($daemon->Kill_Daemon($pf)) {
  50. print "Successfully stopped.\n";
  51. } else {
  52. print "Could not find $pid. Was it running?\n";
  53. }
  54. } else {
  55. print "Not running, nothing to stop.\n";
  56. }
  57. }
  58. sub status {
  59. if ($pid) {
  60. print "Running with pid $pid.\n";
  61. } else {
  62. print "Not running.\n";
  63. }
  64. }
  65. sub run {
  66. if (!$pid) {
  67. print "Starting...";
  68. if ($daemonize) {
  69. # when Init happens, everything under it runs in the child process.
  70. # this is important when dealing with file handles, due to the fact
  71. # Proc::Daemon shuts down all open file handles when Init happens.
  72. # Keep this in mind when laying out your program, particularly if
  73. # you use filehandles.
  74. $daemon->Init;
  75. }
  76. setpriority(0,0,19);
  77. $SPID=~s/\.pl$/\.pid/;
  78. write_to_file($SPID,$$);
  79. my %trash_patterns = (
  80. 'Receive illegal destination ip packet 255.0.0.0 ,drop it' =>'1',
  81. 'Receive illegal destination ip packet 0.0.0.0 ,drop it' =>'1',
  82. 'SD Normal' =>'1',
  83. 'SD Abnormal' =>'1',
  84. 'source:0.0.0.0 destination:0.0.0.0 user:admin cmd:login' =>'1',
  85. 'FAN\'S speed level - 1 changed to level - 0.' => '1',
  86. 'FAN\'S speed level - 0 changed to level - 1.' => '1',
  87. "Environment-I-FANS-SPEED-CHNG: FAN'S speed level"=>'1'
  88. );
  89. my %warning_patterns = (
  90. 'SHUTDOWN-CTRL' => '1',
  91. 'PORT_FLOW' => '1',
  92. 'System ColdStart' => '1',
  93. 'Deny user/' => '1',
  94. 'LOOP-BACK-DETECTED' => 'loop',
  95. 'Find loop' =>'loop',
  96. 'SYS-5-LOOP' => 'loop',
  97. 'drifting from' => 'loop',
  98. 'Port-security has reached' => '1',
  99. 'Unauthenticated IP-MAC' => '1',
  100. 'FAN_FAILED' => '0',
  101. 'has the same IP Address' => '1',
  102. 'Loop detected on port e0' => 'loop',
  103. 'loopguard' => 'zyxel_loop',
  104. 'without management command' => '1',
  105. 'System cold start' =>'1',
  106. 'topology changes' => '1',
  107. 'HMON-0-power'=>'1',
  108. 'On battery power in response to an input power problem'=>'1',
  109. 'No longer on battery power'=>'1',
  110. 'Environment-W-PS-STAT-CHNG'=>'1',
  111. 'System warm start' => '1'
  112. );
  113. while (1) {
  114. eval {
  115. my $db = init_db();
  116. open(SYSLOG,$socket_path) || die("Error open fifo socket $socket_path: $!");
  117. while (my $logline = <SYSLOG>) {
  118. next unless defined $logline;
  119. chomp($logline);
  120. my ($timestamp,$host_ip,$message) = split (/\|/, $logline);
  121. next if (!$message);
  122. $message =~ s/\r/ /g;
  123. $message =~ s/\\015//g;
  124. $message =~ s/\\012//g;
  125. next if (!$message);
  126. next if (!$host_ip);
  127. if (time()-$last_refresh_config>=60) { init_option($db); }
  128. log_debug("Raw message: $message");
  129. #is trash messages?
  130. my $trash = 0;
  131. foreach my $pattern (keys %trash_patterns) {
  132. next if (!$pattern);
  133. if ($message=~/$pattern/i) {
  134. log_debug("Trash pattern: $pattern");
  135. $trash = 1;
  136. last;
  137. }
  138. }
  139. next if ($trash);
  140. my $hostname=$host_ip;
  141. my $netdev = get_device_by_ip($db,$host_ip);
  142. my $id = 0;
  143. if ($netdev) {
  144. $hostname = $netdev->{device_name};
  145. $id = $netdev->{id};
  146. } else {
  147. log_debug("Host with $host_ip is not found in netdevices!");
  148. }
  149. my $q_msg=$db->quote($message);
  150. my $ssql="INSERT INTO remote_syslog(device_id,ip,message) values('".$id."','".$host_ip."',".$q_msg.")";
  151. do_sql($db,$ssql);
  152. foreach my $pattern (keys %warning_patterns) {
  153. next if (!$pattern);
  154. if ($message=~/$pattern/i) {
  155. log_info("Warning pattern $pattern found! Send email.",1);
  156. sendEmail("Syslog warning for $hostname [".$host_ip."]!",$host_ip." ".$message);
  157. last;
  158. }
  159. }
  160. }
  161. close(SYSLOG);
  162. };
  163. if ($@) { log_error("Exception found: $@"); sleep(60); }
  164. }
  165. } else {
  166. print "Already Running with pid $pid\n";
  167. }
  168. }
  169. sub usage {
  170. print "usage: syslog-monitord.pl (start|stop|status|restart)\n";
  171. exit(0);
  172. }
  173. sub reload {
  174. print "reload process not implemented.\n";
  175. }
  176. sub restart {
  177. stop;
  178. run;
  179. }