Monday, May 20, 2013


Unix Memory Hog Finder

Return all processes memory information
subprocess.Popen("ps -u %s -o rss”)
Save a process class into a Parent/Child Structure
Parent
   -- Child
      --Child
        --Child
   -- Child
The process class has information such as current memory size, users, PID, PPID. I also implemented a timestamp and memory size for every process in case a process “bloated” up
Algorithm:
ParentProcessArray = []
For all processes:
   // IF parent PID is one then this is a parent process
   if process.PPID == 1:
      ParentProcessArray.append(process)
      break
   for all in ParentProcessArray:
     if process.PID == ParentProcessArray[i]
      ParentProcessArray.addchild(process)
             break;

// For Recursive Print Function:
// RETURN ALL CHILDREN FROM A PARENT RECURSIVELY
 def GetChildren(self,lvl=0):
        global ListOfChildren
        global Memcount
        if lvl == 0:
            ListOfChildren = []           
            Memcount = self.GetHighestRSS()
        for child in self.ListOfChildProcesses:
            child.lvl = lvl + 1           
            Memcount = Memcount + child.GetHighestRSS()
            ListOfChildren.append(child)
            if child.HasChild():
               child.GetChildren(lvl + 1)
        if lvl == 0:
            self.ParentProcTotalMem = Memcount
            Memcount = 0
        return ListOfChildren

This script logs all of the processes and how much memory is being used on a box. This is a very useful script and the algorithm of how it works is in the snippet above. It iterates through all processes. If it is a parent it puts it in the global parent array. For all processes that is a child of a parent it gets put into a parent with the recursive add child method.


Useful unix commands

#sort file, page output, print
sort file | pr -3 | lp
#write output to file
Cmd > dump
#take input from file
Cmd < file
#show last commands
Fc –l
#grep command
Grep word *.*


These are great unix commands that I use almost daily. I will keep appending to the list as I find new useful unix commands.

Recursive Function for walking down a list and getting children of children


def GetChildren(self,lvl=0):  global ListOfChildren     
  global Memcount             
  if lvl == 0:                ListOfChildren = []        
                 Memcount = self.GetHighestRSS()                 
  for child in self.ListOfChildProcesses:         
  child.lvl = lvl         
  Memcount = Memcount + child.GetHighestRSS()              ListOfChildren.append(child)         
  if child.HasChild():                 child.GetChildren(lvl + 1)     
  if lvl == 0:         
  self.ParentProcTotalMem = Memcount         
  Memcount = 0     
  return ListOfChildren


This is a snippit of using a recursive function that walks down a tree of children with children of their own.

Sorting a List of Lists in Python



ListOfLists[0] = ListList[0] = bob; List[1] = otherstring; List[2] = 3;for list in ListOfLists:    tmp_toup = (int(list[2]),list)      
    ProcessTuple.append(tmp_toup)    #this sorts my tuple_list in order with the list[2] as a key                      
    sorted_tuple_list = sorted(ProcessTuple , key=lambda x: x[0])

This snippit is very useful for sorting a list of lists. One could also use a map for this implementation but if one wants to keep duplicates this is a good option. I have used this snippet many times.

Generic Email Sender




For my generic email sending task I needed to create an easy way for anyone to send emails within the business system. I made my implementation by having an email configuration which could be changed at anytime which then looked at the database for the actual email address. Once it had the address it sent the message.

Estamated Time to complete: 3 days.

Actual time of Completion: 1 week.