bensie (owner)

Fork Of

gist: 160878 by anonymous

Revisions

gist: 160881 Download_button fork
public
Public Clone URL: git://gist.github.com/160881.git
Embed All Files: show embed
Ruby #
1
2
3
4
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
99
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env ruby
 
require 'rubygems'
require 'logger'
require 'net/ssh'
require 'net/smtp'
require 'patron'
require 'timeout'
require 'parseconfig'
require 'yaml'
 
##Globals##
raw_config = File.read('/etc/artemis.yml')
$config = YAML.load(raw_config).symbolize_keys
 
puts @config[:log_location] #$log_file = config.get_value('log_location') #loggin info
$log = Logger.new('log_file', 'monthly')
 
#email settings
puts @config[:email_server] #$email_svr = config.get_value('email_server') #smtp server info
puts @config[:email_from] #$email_from = config.get_value('email_from') #email address in which email is sent from
puts @config[:email_to] #$email_to = config.get_value('email_to') #email address where the errors will be sent
 
svr = config.get_value('servers') #get servers
$svrs = svr.to_s.split(' ') #split the server string into an array
 
$svr_log = config.get_value('server_log')
 
$www_doc = config.get_value('check_html') #get html/php test file
$jsp_doc = config.get_value('check_java') #get jstp test file
 
www = config.get_value('sites') #parseconf only passes strings
$www_sites = www.to_s.split(' ') #split the www string into an array
 
jsp = config.get_value('java_sites') #get jsp/tomcat sites
$jsp_sites = jsp.to_s.split(' ') #split jsp sites the string into an array
 
smtp = config.get_value('smtp_servers') #get smtp servers
$smtp_svr = smtp.to_s.split(' ') #split smtp server string into an array
 
$smtp_log = config.get_value('smtp_logs') #get location of smtp logs
 
 
# Build config
 
 
n = 0
$svrs.each do |s| #creating array of server with hash for the different values
  server[n] = Hash.new
  server[n][:name] = s
  server[n][:status] = true
  server[n][:log] = $svr_log
  n += 1
end
 
n = 0 #reset n to 0 for the iteration
$smtp_svr.each do |s|
  smtp_svr[n] =Hash.new
  smtp_svr[n][:name] = s
  smtp_svr[n][:status] = true
  n += 1
end
 
 
 
#### Temp Clases
 
class Resource
 
  def check(ssh)
  end
 
end
 
 
 
class Server < Resource
  #class Server
  attr_accessor :resources
 
 
  def initialize(resources)
    @resources = resources
  end
 
  ssh = Net::SSH.start("#{@server}", 'tester', :password => "!@retset")
 
  def check(ssh)
    begin
      mnt_check_result = mnt_chk(ssh)
      syslog_check_result = syslog_chk(ssh)
      result = []
 
      if mnt_check_result != true
        result << mnt_check_result
      end
      if syslog_check_result != true
        result << syslog_check_result
      end
 
      unless result
        return true
      else
        return result
      end
    ensure
      ssh.close
    end
  end
 
  private
 
  def mnt_chk(ssh)
 
    uname = ssh.exec!("uname")
 
    if uname.nil?
      return "Failed to connect to #{@server} during mount check."
    else
      mntchk = ssh.exec!("mount | awk '{print $5}' | grep nfs | wc | awk '{print $1}'")
      fstabchk = ssh.exec!("cat /etc/fstab | awk '{print $3}' | grep nfs | wc | awk '{print $1}'")
 
      if mntchk == fstabchk
        true
      else
        #return "A mount on #{a} is down or hung."
       return "A mount on #{@server} is down or hung."
      end
    end
  end
 
 
  def syslog_chk(ssh)
 
    uname = ssh.exec!("uname")
 
    if uname.nil?
      return "Failed to connect to #{@server} during syslog check."
    else
      syserr = ssh.exec!("egrep '(reject|warning|error|fatal|panic|err)' /var/log/messages")
 
      if syserr.nil?
        # true
        return puts "no syslog erros"
      else
        return syserr
      end
    end
  end
 
 
  def smtp_chk(ssh)
 
    uname = ssh.exec!("uname")
 
    if uname.nil?
      return "Failed to connect to #{@server} during smtp check."
    else
      mailerr = ssh.exec!("egrep '(reject|warning|error|fatal|panic|err)' #{$smtp_log}")
 
      if mailerr.nil?
        true
      else
        return mailerr
      end
    end
  end
 
end
 
 
 
### end classes
 
 
#email program
def email_prg(msgstr)
  Net::SMTP.start($email_svr, 25) do |smtp|
    smtp.send_message msgstr, '$email_from', '$email_to'
  end
end
 
 
 
##Daemon##
 
  # loop do
 
  if $svrs != nil
    $svrs.each do
      begin
        Timeout::timeout(10) do
          output = Server.new(resources)
          results = output.check(resources)
      
          results.each do |result|
            #$log.error(result)
            if output == nil
              puts "everything is cool"
            else
              puts result
            end
              
          end
 
        end
      rescue Timeout::Error
        $log.error("Timeout Error: Check network connection and/or NFS mounts on #{resource}")
        puts(email_prg "Timeout Error: Check network connection and/or NFS mounts on #{resource}")
      end
    end
 
    #sleep 120
  end
  # end
 
end