Module: Processwatch

Defined in:
lib/processwatch/pw_linux.rb,
lib/processwatch/version.rb

Overview

License: (MIT), Copyright © 2013 Process Watch Author Phil Chen.

Constant Summary collapse

VERSION =
"3.0.9"

Instance Method Summary collapse

Instance Method Details

#restartObject



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
# File 'lib/processwatch/pw_linux.rb', line 5

def restart

    ps_list = `ps h -eo cmd`
    require 'socket'
    host = Socket.gethostname
    dir = File.dirname("/usr/local/processwatch/conf")
    load File.expand_path("#{dir}/conf/general_restart_contact")

    Dir[File.expand_path("#{dir}/conf/restart_*")]. uniq. each do |file|
      load file

    list = ps_list.split(/\n/)

    occurances = list.grep(/.*#$restart_process.*/)
    if occurances.length == 0 && $restart_mail == "yes" && $restart_start == "yes"
      usw =  Usagewatch
diagnostic_msgstr = <<END_OF_MESSAGE
 #{usw.uw_diskused} Total Gigabytes Disk Used
 #{usw.uw_diskused_perc} Percentage of Gigabytes Used
 #{usw.uw_cpuused}% CPU Used
 #{usw.uw_tcpused} TCP Connections Used
 #{usw.uw_udpused} UDP Connections Used
 #{usw.uw_memused}% Active Memory Used
 #{usw.uw_load} Average System Load Of The Past Minute
 #{usw.uw_bandrx} Mbit/s Current Bandwidth Received
 #{usw.uw_bandtx} Mbit/s Current Bandwidth Transmitted
 #{usw.uw_diskioreads}/s Current Disk Reads Completed
 #{usw.uw_diskiowrites}/s Current Disk Writes Completed
 Top Ten Processes By CPU Consumption: #{usw.uw_cputop}
 Top Ten Processes By Memory Consumption: #{usw.uw_memtop}
END_OF_MESSAGE

restart_msgstr = <<END_OF_MESSAGE
From: #$restart_from <#$restart_from_email>
To: #$restart_to <#$restart_to_email>
Subject: Dead Process Detected On #{host}

Process Watch has detected #{$restart_process} has died.

We have started #{$restart_process} again.

Below Is a Snapshot Of System Statistics Before The Process Was Started Again

#{diagnostic_msgstr}

-Process Watch

END_OF_MESSAGE
        system $restart_action
          require 'net/smtp'
          Net::SMTP.start("#$restart_smtp_host", "#$restart_smtp_port") do |smtp|
          smtp.send_message(restart_msgstr, "#$restart_from_email", "#$restart_to_email")
    end
    elsif occurances.length == 0 && $restart_mail == "yes" && $restart_start == "no"
      usw =  Usagewatch
diagnostic_msgstr = <<END_OF_MESSAGE
 #{usw.uw_diskused} Total Gigabytes Disk Used
 #{usw.uw_diskused_perc} Percentage of Gigabytes Used
 #{usw.uw_cpuused}% CPU Used
 #{usw.uw_tcpused} TCP Connections Used
 #{usw.uw_udpused} UDP Connections Used
 #{usw.uw_memused}% Active Memory Used
 #{usw.uw_load} Average System Load Of The Past Minute
 #{usw.uw_bandrx} Mbit/s Current Bandwidth Received
 #{usw.uw_bandtx} Mbit/s Current Bandwidth Transmitted
 #{usw.uw_diskioreads}/s Current Disk Reads Completed
 #{usw.uw_diskiowrites}/s Current Disk Writes Completed
 Top Ten Processes By CPU Consumption: #{usw.uw_cputop}
 Top Ten Processes By Memory Consumption: #{usw.uw_memtop}
END_OF_MESSAGE

dead_process_msgstr = <<END_OF_MESSAGE
From: #$restart_from <#$restart_from_email>
To: #$restart_to <#$restart_to_email>
Subject: Dead Process Detected On #{host}

Process Watch has detected #{$restart_process} has died.

Below Is A Snapshot Of Current System Statistics

#{diagnostic_msgstr}

-Process Watch

END_OF_MESSAGE
      require 'net/smtp'
      Net::SMTP.start("#$restart_smtp_host", "#$restart_smtp_port") do |smtp|
      smtp.send_message(dead_process_msgstr, "#$restart_from_email", "#$restart_to_email")
    end
    elsif occurances.length == 0 && $restart_mail == "no" && $restart_start == "yes"
      system $restart_action
    end
  end
end

#runawayObject



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
# File 'lib/processwatch/pw_linux.rb', line 100

def runaway

  ps_list = `ps h -eo cputime,pcpu,pid,user,cmd`

  dir = File.dirname("/usr/local/processwatch/conf")

  Dir[File.expand_path("#{dir}/conf/runaway")]. uniq. each do |file|
    load file

    list = ps_list.split(/\n/)
    list.each do |p|
    process = p.split
    process[0] =~ /(\d+):(\d+):(\d+)/

    cpu_time = ($1.to_i*3600.to_i + $2.to_i*60.to_i + $3.to_i)
  next if cpu_time.to_i < $runaway_max_time.to_i
  begin
      if $runaway_mail == "yes" && $runaway_kill == "yes"
        usw =  Usagewatch
diagnostic_msgstr = <<END_OF_MESSAGE
 #{usw.uw_diskused} Total Gigabytes Disk Used
 #{usw.uw_diskused_perc} Percentage of Gigabytes Used
 #{usw.uw_cpuused}% CPU Used
 #{usw.uw_tcpused} TCP Connections Used
 #{usw.uw_udpused} UDP Connections Used
 #{usw.uw_memused}% Active Memory Used
 #{usw.uw_load} Average System Load Of The Past Minute
 #{usw.uw_bandrx} Mbit/s Current Bandwidth Received
 #{usw.uw_bandtx} Mbit/s Current Bandwidth Transmitted
 #{usw.uw_diskioreads}/s Current Disk Reads Completed
 #{usw.uw_diskiowrites}/s Current Disk Writes Completed
 Top Ten Processes By CPU Consumption: #{usw.uw_cputop}
 Top Ten Processes By Memory Consumption: #{usw.uw_memtop}
END_OF_MESSAGE

kill_msgstr = <<END_OF_MESSAGE
From: #$runaway_from <#$runaway_from_email>
To: #$runaway_to <#$runaway_to_email>
Subject: Runaway Process Detected

The Below Runaway Process Has Been Detected

#{process[4]}

It has been killed!

Below Is A Snapshot Of The System Statistics Before It Was Killed

#{diagnostic_msgstr}
END_OF_MESSAGE

        require 'net/smtp'
        Net::SMTP.start("#$runaway_smtp_host", "#$runaway_smtp_port") do |smtp|
        smtp.send_message(kill_msgstr, "#$runaway_from_email", "#$runaway_to_email")
          system("/bin/kill" + " " + "-9" + " " + "#{process[2]}")
      end
      elsif $runaway_mail == "yes" && $runaway_kill == "no"
        usw =  Usagewatch
diagnostic_msgstr = <<END_OF_MESSAGE
 #{usw.uw_diskused} Total Gigabytes Disk Used
 #{usw.uw_diskused_perc} Percentage of Gigabytes Used
 #{usw.uw_cpuused}% CPU Used
 #{usw.uw_tcpused} TCP Connections Used
 #{usw.uw_udpused} UDP Connections Used
 #{usw.uw_memused}% Active Memory Used
 #{usw.uw_load} Average System Load Of The Past Minute
 #{usw.uw_bandrx} Mbit/s Current Bandwidth Received
 #{usw.uw_bandtx} Mbit/s Current Bandwidth Transmitted
 #{usw.uw_diskioreads}/s Current Disk Reads Completed
 #{usw.uw_diskiowrites}/s Current Disk Writes Completed
 Top Ten Processes By CPU Consumption: #{usw.uw_cputop}
 Top Ten Processes By Memory Consumption: #{usw.uw_memtop}
END_OF_MESSAGE

no_kill_msgstr = <<END_OF_MESSAGE
From: #$runaway_from <#$runaway_from_email>
To: #$runaway_to <#$runaway_to_email>
Subject: Runaway Process Detected

The Below Runaway Process Has Been Detected

#{process[4]}

Below Is A Snapshot Of Current System Statistics

#{diagnostic_msgstr}
END_OF_MESSAGE

        require 'net/smtp'
        Net::SMTP.start("#$runaway_smtp_host", "#$runaway_smtp_port") do |smtp|
        smtp.send_message(no_kill_msgstr, "#$runaway_from_email", "#$runaway_to_email")
      end
      elsif $runaway_mail == "no" && $runaway_kill == "yes"
        system("/bin/kill" + " " + "-9" + " " + "#{process[2]}")
      else
        exit
      end
      rescue
        puts "Error Killing the process"
      retry
      end
    end
  end
end