--- ./needs-restarting.py_orig	2017-09-13 03:42:15 -0500
+++ ./needs-restarting.py	2017-09-13 03:44:18 -0500
@@ -43,6 +43,49 @@
 import glob
 import stat
 from optparse import OptionParser
+import subprocess
+import re
+import time
+
+def pid_elapse(pid):
+    etime_proc = subprocess.Popen("ps " + "-p " + pid + " -o etime=",
+                                   stdout=subprocess.PIPE,
+                                   stderr=subprocess.PIPE,
+                                   shell=True)
+    (etime, error) = etime_proc.communicate()
+    elapse_epoch = 0
+    if etime_proc.returncode == 0:
+        etime = etime.replace('-',':')
+        etime_list = etime.split(':')
+        etime_len = len(etime_list)
+
+        (ds, hs, ms, ss, num_secs) = (0, 0, 0, 0, 0)
+
+        if etime_len == 4:
+            ds = int(etime_list[0]) * 86400
+            hs = int(etime_list[1]) * 3600
+            ms = int(etime_list[2]) * 60
+            ss = int(etime_list[3])
+
+        elif etime_len == 3:
+            hs = int(etime_list[0]) * 3600
+            ms = int(etime_list[1]) * 60
+            ss = int(etime_list[2])
+
+        elif etime_len == 2:
+            ms = int(etime_list[0]) * 60
+            ss = int(etime_list[1])
+
+        elif etime_len == 1:
+            if not etime_list[0].isspace():
+                ss = int(etime_list[0])
+
+        num_secs = ds + hs + ms +ss
+        epoch_time = int(time.time())
+        elapse_epoch = epoch_time - num_secs
+        return elapse_epoch
+    else:
+        return -1
 
 def parseargs(args):
     usage = """
@@ -73,24 +116,56 @@
     return pids
 
 def get_open_files(pid):
+    # In AIX /proc/pid/map file contains the map of a running process,
+    # but this file is a binary file and not a text as in case of linux.
+    # So, we need to use the existing tools used to read the /proc files.
     files = []
-    smaps = '/proc/%s/map' % pid
-    try:
-        maps = open(smaps, 'r')
-    except (IOError, OSError), e:
-        print "Could not open %s" % smaps
-        return files
 
-    for line in maps.readlines():
-        if line.find('fd:') == -1:
-            continue
-        line = line.replace('\n', '')
-        slash = line.find('/')
-        filename = line[slash:]
-        filename = filename.replace('(deleted)', '') #only mildly retarded
-        filename = filename.strip()
-        if filename not in files:
-            files.append(filename)
+    line_num = 0
+    procmap_proc = subprocess.Popen("procmap " + pid,
+                                     stdout=subprocess.PIPE,
+                                     stderr=subprocess.PIPE,
+                                     shell=True)
+    for line in procmap_proc.stderr:
+        if re.search("is a kernel process", line):
+            return files
+    grep_proc = subprocess.Popen("grep 'read/exec'",
+                                  stdin=procmap_proc.stdout,
+                                  stdout=subprocess.PIPE,
+                                  shell=True)
+    for line in grep_proc.stdout:
+        line = line.strip().split()
+        if line_num != 0:
+            objects = line[:][3]
+            # .a archive files. Entry returned is libtest.a[shr.o]
+            # but it's better to have the entry as libtest.a as rpm -ql command shows
+            # library as libtest.a.
+            find_a = line[:][3].find('.a')
+            if find_a != -1:
+                a_line = objects[0:find_a+2]
+                files.append(a_line)
+            else:
+                files.append(objects)
+                  
+        elif line_num == 0:
+            ps_proc = subprocess.Popen("ps -o args -p" + pid + "| sed 1d",
+                                        stdout=subprocess.PIPE,
+                                        shell=True)
+            (cmd, error) = ps_proc.communicate()
+            space = cmd.find(' ')
+            cmd = cmd[0:space]
+            slash = cmd.find('/')
+            if slash == -1:
+                os.environ["PATH"]="/opt/freeware/bin:/opt/freeware/sbin:/usr/linux/sbin:/usr/linux/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin"
+                which_proc = subprocess.Popen("which " + cmd,
+                                                 stdout=subprocess.PIPE,
+                                                 stderr=subprocess.PIPE,
+                                                 shell=True)
+                (which_op, error) = which_proc.communicate()
+                if which_proc.returncode == 0:
+                    files.append(which_op)
+        line_num = line_num + 1
+ 
     return files
 
 def main(args):
@@ -110,7 +185,9 @@
 
     for pid in return_running_pids(uid=myuid):
         try:
-            pid_start = os.stat('/proc/' + pid)[stat.ST_CTIME]
+            # getting ctime doesn't seem to be a accurate thing here.
+            # So, get the elpased time since the process start.
+            pid_start = pid_elapse(pid)
         except OSError, e:
             continue
         found_match = False
@@ -125,13 +202,19 @@
 
     for pid in needing_restart:
         try:
-            cmdline = open('/proc/' +pid+ '/cmdline', 'r').read()
+            procmap_proc = subprocess.Popen("procmap " + pid, 
+                                             stdout=subprocess.PIPE,
+                                             shell=True)
+            head_proc = subprocess.Popen("head -n1",
+                                          stdin=procmap_proc.stdout,
+                                          stdout=subprocess.PIPE,
+                                          shell=True) 
+            (output, error) = head_proc.communicate()
         except OSError, e:
             print "Couldn't access process information for %s: %s" % (pid, str(e))
             continue
         # proc cmdline is null-delimited so clean that up
-        cmdline = cmdline.replace('\000', ' ')
-        print '%s : %s' % (pid, cmdline)
+        print output.strip('\n')
         
     return 0
     
