Advertisement
Guest User

Monitor Java Process GC and Restart

a guest
Sep 23rd, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # Script : monitor_java_process.pl
  4. # Version: 0.1
  5. # Author : sunny kim (sunny@kimster.com)
  6. # Date : September 22, 2013
  7. #
  8. # Description
  9. # -----------
  10. # Monitors java garbage collection using jstat and restarts it when it's on the verge
  11. # of OOM'ing. Originally written to handle logstash agent hanging.
  12. #
  13. # Usage
  14. # -----
  15. # perl monitor_java_process.pl --process_name=[string to identify the process from ps]
  16. # --init_name=[name of your init script in /etc/init.d/]
  17. #
  18. #
  19.  
  20. use strict;
  21. use Sys::Syslog;
  22. use Getopt::Long;
  23. use Fcntl ':flock';
  24.  
  25. INIT
  26. {
  27. open *{0} or die "$0:$!";
  28. flock *{0}, LOCK_EX|LOCK_NB or exit;
  29. }
  30.  
  31.  
  32. #--------------------------------------------------------------------
  33. # Global variables
  34. #--------------------------------------------------------------------
  35. my $process_name = "";
  36. my $init_name = "";
  37. my $bad1 = 1;
  38. my $bad2 = 1;
  39. my $threshold = 0.99;
  40. my $duration = 10;
  41.  
  42.  
  43. #--------------------------------------------------------------------
  44. # Syslogging init
  45. #--------------------------------------------------------------------
  46. my $logopt = "ndelay,nofatal,pid";
  47. my $facility = "LOG_USER";
  48. my $identity = $0;
  49. if ($identity =~ /\//) {
  50. my @split = split(/\//,$identity);
  51. $identity = pop @split;
  52. }
  53. # Open log connection to syslog
  54. openlog $identity, $logopt, $facility;
  55.  
  56.  
  57.  
  58. #----------------------------------------------------------
  59. # Handle runtime input parameters
  60. #----------------------------------------------------------
  61. my $param = GetOptions (
  62. "process_name=s" => \$process_name,
  63. "init_name=s" => \$init_name,
  64. );
  65. #----------------------------------------------------------
  66. # get process id
  67. #----------------------------------------------------------
  68. my $pid = `/bin/ps aux | /bin/grep "$process_name" | /bin/grep -v grep | /bin/grep -v monitor_java_process | /usr/bin/awk '{print \$2}'`;
  69. chomp $pid;
  70.  
  71. #----------------------------------------------------------
  72. # start if it doesn't exist
  73. #----------------------------------------------------------
  74. if (! $pid) {
  75. my $out = `/usr/sbin/service $init_name start`;
  76. `/bin/echo "$out" >> /tmp/service.txt`;
  77. syslog("warning", "started $init_name");
  78. exit;
  79. }
  80.  
  81. #----------------------------------------------------------
  82. # run jstat
  83. #----------------------------------------------------------
  84. my @jstat = `/usr/bin/jstat -gc $pid 1000 $duration`;
  85.  
  86. #----------------------------------------------------------
  87. # evaluate jstat result
  88. #----------------------------------------------------------
  89. FOR:foreach my $jstat (@jstat) {
  90. next FOR if ($jstat =~/S0C/);
  91. my @split = split(/\s+/,$jstat);
  92.  
  93. my $pct1 = int($split[2]) ? $split[2] / $split[0] : 0;
  94. my $pct2 = int($split[3]) ? $split[3] / $split[1] : 0;
  95.  
  96. if ($pct1 < $threshold) {
  97. $bad1 = 0;
  98. }
  99.  
  100. if ($pct2 < $threshold) {
  101. $bad2 = 0;
  102. }
  103.  
  104. print "($split[0] $split[1] $split[2] $split[3]) percentage: ($pct1) ($pct2)\n";
  105. }
  106.  
  107. #----------------------------------------------------------
  108. # kill and restart if the process is bad
  109. #----------------------------------------------------------
  110. if ($bad1 || $bad2) {
  111. print "killing process $pid\n";
  112. `/bin/kill -9 $pid`;
  113. sleep(10);
  114. print "restarting $init_name\n";
  115. my $out = `/usr/sbin/service $init_name start`;
  116. `/bin/echo "$out" >> /tmp/service.txt`;
  117. syslog("warning", "restarted $init_name");
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement