Archive - python RSS Feed

python generate graphs using cairoplot

generating graphs using cairo was relatively very easy, i ran this on a debian box. First you need to install “bazaar”

root@server:/usr/src/python# apt-get install bzr

you might need python-cairo and other dependencies..

root@server:/usr/src/python# apt-get install python-cairo python-cairo-dev python-cairo-dbg python-pycha python-gasp python-docky

Continue Reading…

 

python subprocess.Popen example

An example on how to use python’s subprocess.Popen function:

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
#!/usr/bin/env python
import subprocess
import os
#import pdb; pdb.set_trace()

def find():
    y = raw_input("Enter a text you want to search on the system!")
    s = y.rstrip("\n")
    print "string", s
    #    command = "find /bin -name " + str(y)
    #    arg = ['/usr/bin/find', '/bin', '-name', s '-print']
    #    print "command", command
    find = subprocess.Popen([r"/usr/bin/find", "/", "-name", y, "-print"], stdout=subprocess.PIPE)
    #find_stdout = find.communicate()[0]
    #print (find_stdout)
    for line in find.stdout.readlines():
        print "line", line
        l = line.rstrip("\n")
        list = subprocess.Popen([r"ls", "-l", l], stdout=subprocess.PIPE)
        list_stdout = list.communicate()[0]
        print list_stdout
    file = subprocess.Popen([r"file", l], stdout=subprocess.PIPE)
    file_stdout = file.communicate()[0]
    print file_stdout

def main():
    find()

main()

You can also catch stdout and stderr using subprocess below is a simple example…

1
2
3
4
5
6
7
>>> import subprocess
>>> proc = subprocess.Popen([r"ls /etc /etc/nonexistant"], shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
>>> stdout,stderr = proc.communicate()
>>> print stdout
blah blah ..output from ls stripped...
>>> print stderr
ls: cannot access /etc/nonexistant: No such file or directory

more examples:

1
2
3
4
5
>>> import subprocess
>>> proc = subprocess.Popen([r"uname -a"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> stdout,stderr = proc.communicate()
>>> print stdout
Linux cce-syslog-001.ceremonies.local 2.6.32-71.29.1.el6.x86_64 #1 SMP Mon Jun 27 19:49:27 BST 2011 x86_64 x86_64 x86_64 GNU/Linux

you can feed the standard output of one command into the standard input of another command, this example shows you how:

1
2
3
4
5
6
7
8
9
10
11
>>> import subprocess
>>> proc = subprocess.Popen("cat /etc/passwd", shell=True, stdout=subprocess.PIPE,stdin=subprocess.PIPE)
>>> filter = subprocess.Popen("grep root ", shell=True, stdin=proc.stdout,stdout=subprocess.PIPE)
>>> for i in filter.stdout:
...    print i
...
root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

>>>
 

python: system monitoring script

This is a simple python system monitoring script using subprocess.call and os.popen function, since they use the internal unix commands to poll the server, if you want a portable script which would work across different OS then psutils is more suitable http://code.google.com/p/psutil/

But the advantage of this script is that it can calculate memory and cpu per process for apache,bind etc…

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
#!/usr/bin/env python

from subprocess import Popen, PIPE, STDOUT
import subprocess
import os
import popen2

def memory():
    print "gathering memory info..."
    subprocess.call("free", shell=True)

def cpu():
    print "gathering system uptime info... "
    subprocess.call("uptime", shell=True)

def pci():
    print "gathering pci devices.."
    subprocess.call("lspci", shell=True)

def apache():
    print "calculating apache mem use.."
    sum = 0.0
    command = os.popen("ps aux | grep apache | awk '{print $4}'")
    for i in command.readlines():
    l = i.rstrip("\n")
    d = float(l)
    sum = d + sum
    print sum

def exim():
    print "calculating exim mem use..."
    esum = 0.0
    ecommand = os.popen("ps aux | grep -v grep | grep exim | awk '{print $4}'")
    for e in ecommand.readlines():
    f = e.rstrip("\n")
    g = float(f)
    esum = g + esum
    print esum

def spam():
    print "calculating spamd mem use.."
    ssum = 0.0
    spamd = os.popen("ps aux | grep spamd | awk '{print $4}'")
    for s in spamd.readlines():
    t = s.rstrip("\n")
    u = float(t)
    ssum = u + ssum
    print ssum

def log():
    print "logged in users..."
    subprocess.call("last| grep 'still'", shell=True)

def main():
    memory()
    cpu()
#   pci()
    apache()
    spam()
    exim()
    log()

main()
 
Page 2 of 2«12
Theme Tweaker by Unreal