Advertisement
Guest User

daywalker rewrite

a guest
Jun 21st, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.08 KB | None | 0 0
  1. use warnings;
  2. use strict;
  3. use Sys::Statistics::Linux;
  4. use Net::SMTP::SSL;
  5. $0='/dev/null/dayWalKeR';
  6.  
  7. #------------------------#
  8. # Declare some variables #
  9. #------------------------#
  10. my $to='someemail';
  11. my $subject='Report-Information';
  12. my $email_pass='pass';
  13. my $from='someotheremail';
  14. my $smtp;
  15. my $count=0;
  16. #---------------------------------------#
  17. if(fork())
  18. {
  19.     close(STDOUT);
  20.     close(STDERR);
  21.     close(STDIN);
  22.     exit;
  23. }
  24. #
  25. while(1)
  26. {
  27. #------------------------#
  28. # Initialize the module  #
  29. #------------------------#
  30. my $lxs = Sys::Statistics::Linux->new(
  31.         sysinfo   => 1,
  32.         cpustats  => 1,
  33.         procstats => 1,
  34.         memstats  => 1,
  35.         netstats  => 1,
  36.         sockstats => 1,
  37.         loadavg   => 1,
  38.     );
  39. sleep 1; my $stat = $lxs->get;
  40. #---------------------------------------#
  41.  
  42. #-------------------------#
  43. # Initialize the objects  #
  44. #-------------------------#
  45. # get cpu stats
  46. my $cpu  = $stat->cpustats->{cpu};
  47. my $cpu_user_perc   = $cpu->{user};
  48. my $cpu_nice_perc   = $cpu->{nice};
  49. my $cpu_system_perc = $cpu->{idle};
  50. my $cpu_idle_perc   = $cpu->{system};
  51. my $cpu_iowait_perc = $cpu->{iowait};
  52.  
  53. #---------------------------------------#
  54. # get loadavg stats
  55. #--------------------#
  56. my $loadavg_1       = $stat->loadavg->{avg_1};
  57. my $loadavg_5       = $stat->loadavg->{avg_5};
  58. my $loadavg_15      = $stat->loadavg->{avg_15};
  59. #my $load=$loadavg_1;
  60. #$load =~ s/\.//;
  61. #---------------------------------------#
  62. # get mem stats in MB
  63. #--------------------#
  64. my $mem_used    = int($stat->memstats->{memused}/1024);
  65. my $mem_free    = int($stat->memstats->{memfree}/1024);
  66. my $mem_total   = int($stat->memstats->{memtotal}/1024);
  67. #
  68. # => to_do
  69. #
  70. my $mem_buffers = int($stat->memstats->{buffers}/1024);
  71. my $mem_cache  = int($stat->memstats->{cached}/1024);
  72. # my $swap_used  = int($stat->memstats->{swap_used}/1024);
  73. # my $swap_free  = int($stat->memstats->{swap_free}/1024);
  74. # my $swap_total = int($stat->memstats->{swap_total}/1024);
  75. #if(-z $mem_buffers or $mem_buffers==0)
  76. #{
  77. #    print "dobro";
  78. #}
  79. # => end to_do
  80. #
  81. #---------------------------------------#
  82. # get proc status
  83. #--------------------#
  84. my $wait_io_proc    = $stat->procstats->{blocked};
  85. my $running_proc    = $stat->procstats->{running};
  86. #---------------------------------------#
  87. # get socket statistics
  88. #--------------------#
  89. my $socket_used     = $stat->sockstats->{used};
  90. my $socket_tcp      = $stat->sockstats->{tcp};
  91. my $socket_udp      = $stat->sockstats->{udp};
  92. #---------------------------------------#
  93. # get SysInfo
  94. #--------------------#
  95. my $hostname         = $stat->sysinfo->{hostname};
  96. my $kernel_name      = $stat->sysinfo->{kernel};
  97. my $kernel_release   = $stat->sysinfo->{release};
  98. my $uptime           = $stat->sysinfo->{uptime};
  99. my $idle_time        = $stat->sysinfo->{idletime};
  100.  
  101. #---------------------------------------#
  102. #
  103.     if( $loadavg_1 > 0.30 || $mem_free < 100 )
  104.     {
  105.         my $hrule = "\n";
  106.         my $hrule2 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
  107.         my $sys_info_msg = " Hostname: '$hostname' - Kernel: '$kernel_name $kernel_release' - Uptime: '$uptime' - Idling: '$idle_time'\n";
  108.         my $load_avg_msg = " Load Average: [$loadavg_1, $loadavg_5, $loadavg_15] | RAM used:[$mem_used MB] - free:[$mem_free MB] - total:[$mem_total MB] - cache:[$mem_cache MB] - buffer:[$mem_buffers MB]\n";
  109.         my $cpu_stat_msg = " CPU Usage statistics:\n user usage: [$cpu_user_perc\%] - nice usage: [$cpu_nice_perc\%] - system usage: [$cpu_system_perc\%] - I/O usage: [$cpu_iowait_perc\%] - idle usage: [$cpu_idle_perc\%]\n";
  110.         my $proc_stat_msg = " Processes on I/O: [$wait_io_proc] - Running processes: [$running_proc] - Sockets used: [$socket_used] - TCP Sockets: [$socket_tcp] - UDP Sockets: [$socket_udp]\n";
  111.         my $proc_ram_cpu = `ps -eo user,stat,rss,vsz,pid,cputime,cmd --width 100 --sort rss,vsz`;
  112.         my $active_connections = `netstat -anlp | grep ESTABLISHED`;
  113.         my $ps_output = `ps auwwxf`;
  114.         my $top_snap = `top -n 1 -b`;
  115.  
  116.         my $message = $hrule2 . $sys_info_msg . $hrule . $load_avg_msg . $hrule . $cpu_stat_msg . $hrule . $proc_stat_msg . $hrule2 . "\n";
  117.         $message = $message . " Active connections:\n" . $hrule . $active_connections . $hrule . $hrule2 . $hrule . " Top RAM hungry processes:\n" . $hrule . $proc_ram_cpu . "\n" . $hrule2 . "\n" .  " Ps auwwwxf output:\n" . $hrule . $ps_output . $hrule . $hrule2 . $hrule . " top -n1 -b snapshot:\n\n" . $top_snap . $hrule . $hrule2;
  118.        
  119.         send_message($from, $to, $subject, $message);
  120.  
  121.         $message = "";
  122.         $hrule=$hrule2=$sys_info_msg=$load_avg_msg=$cpu_stat_msg=$message=$proc_stat_msg=$proc_ram_cpu=$active_connections=$ps_output=$top_snap=$stat = undef;
  123.  
  124.         if($count<=5)
  125.         {
  126.             $count=$count+1;
  127.             sleep (15);
  128.         }
  129.         $count=0;
  130.     }
  131.  
  132.     $hostname=$kernel_name=$kernel_release=$uptime=$idle_time=$socket_used=$socket_tcp=$socket_udp=$wait_io_proc=$running_proc=$mem_buffers=$mem_cache=$mem_used=$mem_free=$mem_total=$loadavg_1=$loadavg_5=$loadavg_15=$stat=$cpu_user_perc=$cpu_system_perc=$cpu_idle_perc=$cpu_iowait_perc=$cpu = undef;
  133.  
  134.     sleep (30);
  135.  
  136. }
  137.  
  138. sub send_message
  139. {
  140.     my ($from, $to, $subject, $message) = @_;
  141.  
  142.     if(not $smtp = Net::SMTP::SSL->new('smtp.gmail.com',Port => 465, Debug => 0))
  143.     {
  144.         die "Could not connect to mail server\n";
  145.     }
  146.     $smtp->auth($from, $email_pass) || die "Authentication failed!\n";
  147.  
  148.     $smtp->mail($from . "\n");
  149.     my @recipients = split(/,/, $to);
  150.     foreach my $recipient (@recipients)
  151.     {
  152.         $smtp->to($recipient . "\n");
  153.     }
  154.  
  155.     $smtp->data();
  156.     $smtp->datasend("From: " . $from . "\n");
  157.     $smtp->datasend("To: " . $to . "\n");
  158.     $smtp->datasend("Subject: " . $subject . "\n");
  159.     $smtp->datasend("MIME-Version: 1.0\n");
  160.     $smtp->datasend("Content-Type: text/plain; charset=iso-8859-1\n");
  161.     $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
  162.     $smtp->datasend($message . "\n\n");
  163.     $smtp->dataend();
  164.     $smtp->quit;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement