Wednesday, February 29, 2012

Collect bounced emails from postfix

# A ruby script for collecting bounced email with status
require 'rubygems'
path = "/var/spool/postfix/defer"
defer = Dir.foreach("/var/spool/postfix/defer")

defer.each do |x|
  x.gsub("..",'').gsub(".",'')
  if File.directory?("#{path}/#{x}")
     if Dir["#{path}/#{x}"].empty?
       # "is empty"
     else
       # "is not empty"
       a = x.gsub("..",'').gsub(".",'')
       if a == ''
         #
       else
         sub_dir = "#{path}/#{a}"
         Dir.foreach("#{sub_dir}") do |doc|
            sub_sub_dir = doc.gsub("..",'').gsub(".",'')
           
            if sub_sub_dir  == ''
            #
            else
               contents = Array.new
               file = File.open("#{sub_dir}/#{sub_sub_dir}", "rb")
               file.each do |data|
                  contents << data
               end
               output = Array.new
               arr = Array.new
               arr = contents.to_a.join
               arr.split("\n").each do |data|
                  if data =~ /^recipient=/
                    output << data
                  end
                  if data =~ /^status/
                    output << data
                  end
                  if data =~ /^reason/
                      output << data
                  end
                 
                     
               end
               puts " #{output[0]} | #{output[1]} | #{output[2]}"
               puts "!"  
            end
         end     
      
       end   
     end   
    
  else
     #
  end
end

Friday, January 6, 2012

RUBY CLIENT SERVER MONITOR


require "rubygems"
require "socket"
require "openssl"
require "base64"
include OpenSSL
require "base64"
PORT = 5534

chat_client = TCPSocket.new("IP", PORT)


# while line  = gets do chat_client.puts(line) end
$dpipe = `hostname`
$dpipe = $dpipe.strip #squeeze.gsub(""," ")
def load_check
load_check = `uptime | cut -d: -f5 `
  $dpipe = $dpipe + "|"
  load ="#{load_check}".split(" ")
    load.each do
    |lcheck|
    $dpipe = $dpipe + lcheck #.squeeze.gsub("\n","")
  end
end
def disk_check
array=`df -Ph | grep -r '/' | awk '{ printf "%s-%s-%s-%s ",$3,$4,$5,$6  }'`
  bulk_data="#{array}".split(" ")
  $dpipe = $dpipe + "|"
    bulk_data.each do
      |check|
      data="#{check}".split("-")
      max =  data[2]
      if  max.to_i > 90

        $dpipe = $dpipe + "Urgent:" + data[3] + ":" + data[2] + ":" +  data[0]  + ":" + data[1] + ","
      elsif max.to_i < 90 && max.to_i > 75
        $dpipe =   $dpipe + "Warning:" + data[3] + ":" + data[2] + ":" +  data[0]  + ":" + data[1] + ","
      else
       
      end
   end
end
def apache_check
apache_pid = `ps -efa | grep httpd | grep -v grep | awk '{ print $2 }'| xargs |wc -w`
#puts apache_pid.to_i
if  apache_pid.to_i > 1

$dpipe =  $dpipe + "|"
$dpipe =  $dpipe + "httpd:OK"
else
$dpipe =  $dpipe + "|"
$dpipe =  $dpipe + "httpd:NA"
end
end

def mysql_check
mysql_pid = `ps -efa | grep mysql | grep -v grep | awk '{ print $2 }'| xargs |wc -w`
        if  mysql_pid.to_i > 1

                $dpipe =  $dpipe + "|"
                $dpipe =  $dpipe + "mysqld:OK"
        else
                $dpipe =  $dpipe + "|"
                $dpipe =  $dpipe + "mysqld:NA"
end
end

def ram_check
m_array = `free -tom |xargs |cut -d ":" -f 2 |cut -d " " -f 1-7 |xargs`
mem_array="#{m_array}".split(" ")
threshold = ( ( mem_array[1].to_i - mem_array[5].to_i - mem_array[4].to_i ) * 100 ) / mem_array[0].to_i
if  threshold.to_i > 90
   $dpipe =  $dpipe + "|"
   $dpipe =  $dpipe + "RAM:#{threshold.to_i}%:NA"
else
   $dpipe =  $dpipe + "|"
   $dpipe =  $dpipe + "RAM:#{threshold.to_i}%:ok"
end
end
def process_check
hallow = []
  p_array = `ps -eo pid,comm,%cpu,%mem | awk '{OFS="!"; print $1,$2,$3,$4}' | grep -v 'PID!COMMAND!%CPU!%MEM' |xargs `
  proc_list = "#{p_array}".split(" ")
  proc_list.each do |l|
    k = "#{l}".split("!")
   k.each do |lt|
    if  k[2] != "0.0" or k[3] != "0.0"
if k[2].to_i > 50 or k[3].to_i > 20
     hallow << l + ","
end
    end
   end
  end
  if hallow.length < 1
  $dpipe =  $dpipe + "|"
  $dpipe =  $dpipe + "10PID:ok"
  else
  $dpipe =  $dpipe + "|"
           $dpipe =  $dpipe + "10PID:#{hallow}"

  end
end
load_check
disk_check
apache_check
mysql_check
ram_check
process_check
$dpipe.chomp
#enc  = Base64.encode64($dpipe)
puts $dpipe
hu = $dpipe
#chat_client.puts(enc)
#chat_client.puts($dpipe)
chat_client.puts(hu)