-
Notifications
You must be signed in to change notification settings - Fork 4
/
utilities.py
executable file
·139 lines (107 loc) · 3.3 KB
/
utilities.py
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
139
import subprocess
import os
import socket
import commands
from gettext import gettext as _
class Utilities():
"""Utilities
"""
# I18N
_IPS = _('IPs')
_PROCESSID = _('Process ID')
_HOSTNAME = _('Hostname')
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)
subprocess.call(cmd, shell=False)
def getNetworkProcessInfo(self,programName):
status = self.checkProgramStatus(programName)
pid = ""
if status [ 0 ]:
pid = ",".join(status[1])
txt = self._PROCESSID + " = " + pid
txt += "\n"
txt += self._HOSTNAME + " = " + self.getHostname()
txt += "\n"
txt += self._IPS + " = " + self.getNetworkInfo()
return txt