# -*- coding: utf-8 -*-
#!/usr/bin/python
##-------------------------------------------------------------------
## @copyright 2017 DennyZhang.com
## Licensed under MIT 
##   https://raw.githubusercontent.com/DennyZhang/devops_public/master/LICENSE
##
## File : check_out_of_memory.py
## Author : Denny <denny@dennyzhang.com>
## Description : Detect whether OOM(Out Of Memory) has happened in the previous several hours
## --
## Created : <2017-02-28>
## Updated: Time-stamp: <2017-03-14 15:11:52>
##-------------------------------------------------------------------
# Check: http://www.dennyzhang.com/monitor_oom/
import argparse
import platform
import sys
import subprocess

NAGIOS_OK_ERROR=0
NAGIOS_EXIT_ERROR=2

def get_date_from_dmsg(dmsg_entry):
    # From: [Sat Mar 11 00:19:44 2017] java invoked oom-killer: gfp_mask=0x26000c0, order=2, oom_score_adj=-17
    # To: Sat Mar 11 00:19:44 2017
    l = dmsg_entry.split("] ")
    date = l[0][1:]
    return date

def get_oom_entry():
    oom_list = []
    # Sample output
    '''
root@bematech-es-1:~# dmesg -T | grep -i oom
[Sat Mar 11 00:19:44 2017] java invoked oom-killer: gfp_mask=0x26000c0, order=2, oom_score_adj=-17
[Sat Mar 11 00:19:44 2017]  [<ffffffff81188b35>] oom_kill_process+0x205/0x3d0
[Sat Mar 11 00:19:44 2017] [ pid ]   uid  tgid total_vm      rss nr_ptes nr_pmds swapents oom_score_adj name
    '''
    command = 'dmesg -T | grep -i oom'
    print command
    p = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)
    while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()
        oom_list.append(out)
    return oom_list

def filter_entry_by_datetime(oom_list, datetime, hours_to_check):
    ret_list = []
    return ret_list

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--hours_to_check', required=False, default=4, type=int \
                        help="Only check oom entries happen within previous several hours")
    l = parser.parse_args()
    hours_to_check = l.hours_to_check

    # Check OS release
    if platform.linux_distribution()[0] != 'Ubuntu':
        print "ERROR: current only support Ubuntu OS."
        sys.exit(NAGIOS_EXIT_ERROR)

    oom_list = get_oom_entry()
    current_datetime = '' # TODO
    matched_oom_list = filter_entry_by_datetime(oom_list, current_datetime, hours_to_check)
    if len(matched_oom_list) == 0:
        print "OK: No OOM has happened in previous %d hours." % (hours_to_check)
    else:
        print "ERROR: OOM has happened in previous %d hours.\n%s" % \
            (hours_to_check, "\n".join(matched_oom_list))
        sys.exit(NAGIOS_EXIT_ERROR)

    sys.exit(NAGIOS_OK_ERROR)
## File : check_out_of_memory.py ends

@@ -487,13 +487,13 @@
  15:
-11:52
+20:25
 %3E%0A##
@@ -668,16 +668,28 @@
 bprocess
+%0Aimport time
 %0A%0ANAGIOS
@@ -729,20 +729,28 @@
 def get_
-date
+time_seconds
 _from_dm
@@ -890,32 +890,18 @@
 To: 
-Sat Mar 11 00:19:44 2017
+1489191584
 %0A   
@@ -959,20 +959,76 @@
  return 
-date
+int(time.mktime(time.strptime(date,'%25a %25b %25d %25H:%25M:%25S %25Y')))
 %0A%0Adef ge
@@ -1814,24 +1814,31 @@
 m_list, 
-datetime
+current_seconds
 , hours_
@@ -1862,24 +1862,311 @@
 t_list = %5B%5D%0A
+    seconds_per_hour = 3600%0A    current_seconds = int(round(time.time()))%0A    for entry in oom_list.spli(%22%5Cn%22):%0A        entry_seconds = get_time_seconds_from_dmsg(entry)%0A        if current_seconds %3C = entry_seconds + hours_to_check * seconds_per_hour:%0A            ret_list.append(entry)%0A
     return r
@@ -2684,41 +2684,8 @@
 y()%0A
-    current_datetime = '' # TODO%0A
     
@@ -2741,26 +2741,8 @@
 ist,
- current_datetime,
  hou

implement logic