-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower.py
159 lines (126 loc) · 5.52 KB
/
power.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
import subprocess
import configparser
from time import sleep
import logging
from logging.handlers import RotatingFileHandler
from deamon import daemon
from stopper import Stopper
class PowerPy(daemon):
def __init__(self, pidfile):
super().__init__(pidfile)
self.observedPorts = {"TCP": [], "UDP": []}
self.observationInterval = None
self.inactiveTime = None
self.haltCommand = None
self.debug = None
self.debugLogger = None
self.uptimeLogger = None
self.__setUpLoggers()
self.__readConf()
self.stopper = Stopper()
def run(self):
while True:
if self.debug:
self.__printDebug()
isConnectionActive = self.__examinePorts(self.__getActivePorts())
if isConnectionActive:
self.stopper.reset()
else:
self.stopper.progress()
if self.stopper.timeSpent > self.inactiveTime:
self.uptimeLogger.info("POWER DOWN")
self.__runCommand(self.haltCommand)
self.stopper.reset()
self.uptimeLogger.info("POWER UP")
sleep(self.observationInterval)
def __setUpLoggers(self):
# Create the Logger
self.debugLogger = logging.getLogger(__name__."-DEBUG")
self.debugLogger.setLevel(logging.DEBUG)
self.uptimeLogger = logging.getLogger(__name__."-UPTIME")
self.debugLogger.setLevel(logging.DEBUG)
# Create the Handler for logging data to a file
debug_logger_handler = RotatingFileHandler('./logs/debug.log', mode='a', maxBytes=5*1024*1024, backupCount=2, encoding=None, delay=0)
debug_logger_handler.setLevel(logging.DEBUG)
uptime_logger_handler = RotatingFileHandler('./logs/uptime.log', mode='a', maxBytes=5*1024*1024, backupCount=2, encoding=None, delay=0)
uptime_logger_handler.setLevel(logging.DEBUG)
# Create a Formatter for formatting the log messages
logger_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add the Formatter to the Handler
debug_logger_handler.setFormatter(logger_formatter)
uptime_logger_handler.setFormatter(logger_formatter)
# Add the Handler to the Logger
self.debugLogger.addHandler(debug_logger_handler)
self.uptimeLogger.addHandler(uptime_logger_handler)
def __readConf(self):
config = configparser.ConfigParser()
config.read("config.ini")
self.inactiveTime = config["BASIC"].getint("inactiveTime")
self.observationInterval = config["BASIC"].getint("checkInterval")
self.debug = config["BASIC"].getint("debug")
self.haltCommand = config["BASIC"]["haltCommand"]
for protocol in ["TCP", "UDP"]:
if protocol in config:
self.observedPorts[protocol] = [val for key, val in config[protocol].items()]
if "TCP/UDP" in config:
for key, val in config['TCP/UDP'].items():
if val not in self.observedPorts["TCP"]:
self.observedPorts["TCP"].append(val)
if val not in self.observedPorts["UDP"]:
self.observedPorts["UDP"].append(val)
def __getActivePorts(self):
output = self.__runCommand("netstat -antu | grep ESTABLISHED").decode('UTF-8')
return self.__processOutputPorts(output)
def __runCommand(self, command):
#out = subprocess.check_output(command.split())
#print(out)
ps = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
out = ps.communicate()[0]
return out
def __processOutputPorts(self, output):
"""
Connection Indexes:
0 - tcp/udp
1 - Recv-Q
2 - Send-Q
3 - Local Address (IP:PORT)
4 - Foreign Address
5 - State (LISTEN / ESTABLISHED / TIME_WAIT / CLOSE_WAIT)
"""
ports = {"TCP":[], "UDP":[]}
for line in str(output).splitlines():
connection = line.split()
ip = connection[3]
ports[str(connection[0]).upper()].append(ip.split(":")[1])
return ports
def __examinePorts(self, activePorts):
for protocol in activePorts.keys():
for port in activePorts[protocol]:
if port in self.observedPorts[protocol]:
if self.debug:
self.debugLogger.debug("Active port: {}".format(port))
return True
return False
def __printDebug(self):
self.debugLogger.debug("Observed ports: {}".format(self.observedPorts))
self.debugLogger.debug("Active ports: {}".format(self.__getActivePorts()))
self.debugLogger.debug("Matched ports: {}".format(self.__examinePorts(self.__getActivePorts())))
self.debugLogger.debug("Stopper time: {}".format(self.stopper.timeSpent))
self.debugLogger.debug("\n\n ------------------------ \n\n")
if __name__ == "__main__":
if len(sys.argv) == 3:
daemon = PowerPy(sys.argv[2])
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
else:
sys.stdout.write("Unknown command")
sys.exit(2)
sys.exit(0)
else:
sys.stdout.write("usage: {} start|stop|restart".format(sys.argv[0]))
sys.exit(2)