Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utilities.py
blob: ef79ee3f1f1d7ad5e917784fb063a4e3e4926565 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import subprocess
import os
import socket
import commands
import i18n

class Utilities():
    """Utilities
    """

    def __init__(self):
        """Constructor
        """
        pass

    def checkProgramStatus(self, programName):
        """
        Check if 'programName' is running
        Returns: [ A, [ B ] ]
        A: True | False if the program is running
        B: list of PID
        """
        result = []

        ps = subprocess.Popen(["pidof",programName],stdout=subprocess.PIPE)
        pid = ps.communicate()[0].strip().split(" ")
        ps.stdout.close()
        pids = []

        for p in pid:
            p = p.strip()

            if p != "":
                pids.append(p)

        if len(pids) > 0:
            return [True, pids]

        return [False, []]

    def getHostname(self):
        """Get server name
        """
        return socket.gethostname()

    def getNetworkInterfaces(self):
        """Get server network interfaces names
        """
        f = open('/proc/net/dev', 'r')
        lines = f.readlines()
        f.close()
        lines.pop(0)
        lines.pop(0)

        interfaces = []
        for line in lines:
            interface = line.strip().split(" ")[0].split(":")[0].strip()
            interfaces.append(interface)

        return interfaces

    def getNetworkIPs(self, interfaces):
        """Get server IPs per interface
        """
        pattern = "inet addr:"
        cmdName = "/sbin/ifconfig"
        ips = {}

        for interface in interfaces:
            cmd = cmdName + " " + interface
            output = commands.getoutput(cmd)
            inet = output.find(pattern)

            if inet >= 0:
                start = inet + len(pattern)
                end = output.find(" ", start)

                ip = output[start:end]
                ips[interface] = ip
            else:
                ips[interface] = ""

        return ips

    def getNetworkInfo(self):
        """Get server network map {IFACE:IP}
        """
        info = ""

        interfaces = self.getNetworkInterfaces()
        ips = self.getNetworkIPs(interfaces)

        for interface, ip in ips.iteritems():
            if info != "":
                info += "\n         "

            info += interface + ": " + ip

        return info

    def endProgram(self, programName):
        status = self.checkProgramStatus(programName)

        if status [ 0 ] == True:
            pids = status[1]
            for pid in pids:
                os.system("kill -9 " + pid)

    def startProgram(self, programName, args=[]):
        fname = "/usr/bin/" + programName
        if not os.path.isfile(fname):
            fname = "./" + programName

        cmd = [fname]

        if len(args) > 0:
            for arg in args:
                cmd.append(arg)

        f = open("/tmp/ariel.txt", "a")
        f.write(str(cmd) + "\n" )
        f.close()

        subprocess.call(cmd, shell=False)

    def getNetworkProcessInfo(self,programName):
        status = self.checkProgramStatus(programName)
        pid = ""
        if status [ 0 ]:
            pid = ",".join(status[1])

        txt = i18n.PROCESSID + " = " + pid
        txt += "\n"
        txt += i18n.HOSTNAME + " = " + self.getHostname()
        txt += "\n"
        txt += i18n.IPS + " = " + self.getNetworkInfo()

        return txt