-
Notifications
You must be signed in to change notification settings - Fork 1
/
activityLog.py
207 lines (162 loc) · 5.8 KB
/
activityLog.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/env python
'''RHEL Distributed Python Core'''
import argparse, sys, os, shlex, subprocess, tarfile, time
'''Let's get into the real DB management.'''
import sqlalchemy
'''Why not let it phone home when something goes arye.'''
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
'''General Linux Related Functions'''
def get_hostname():
try:
with open("/etc/hostname", "r") as hostnamefile:
return hostnamefile.read
except IOError:
print("Unable to open /etc/hostname.")
return False
return False
def root_fs_mountable():
try:
with open("/proc/mounts", "r") as mountfile:
for line in mountfile:
if " / " in line and " rw," in line: return True
except IOError:
print("Unable to open /proc/mounts.")
return False
return False
def test_file_writable():
try:
with open("/activityLog_test", "w") as testfile:
testfile.write("This is only a test.")
os.remove("/activityLog_test")
return True
except IOError:
print("Unable to create test file in /.")
return False
return False
def get_loadavgs(): return os.loadavg()
def run_shell_cmd(cmd):
args = shlex.split(cmd)
# This is a blocking shell command call.
subprocess.call(args, stdout=open('/root/activityLog/shellcmd-run.log', 'w'))
with open("/root/activityLog/shellcmd-run.log", "r") as shellfile:
data = shellfile.readlines()
os.remove("/root/activityLog/shellcmd-run.log")
return data
def get_top_data():
try:
with open("/root/.toprc", "r") as topfile:
data = topfile.readlines()
except IOError:
data = [""]
with open("/root/.toprc", "w") as topfile:
topfile.write('RCfile for \"top with windows\"\n')
topfile.write("Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.000, Curwin=0\n")
topfile.write("Def fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX\n")
topfile.write(" winflags=30137, sortindx=10, maxtasks=0\n")
topfile.write(" summclr=1, msgsclr=1, headclr=3, taskclr=1\n")
topfile.write("Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX\n")
topfile.write(" winflags=62777, sortindx=0, maxtasks=0\n")
topfile.write(" summclr=6, msgsclr=6, headclr=7, taskclr=6\n")
topfile.write("Mem fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX\n")
topfile.write(" winflags=62777, sortindx=13, maxtasks=0\n")
topfile.write(" summclr=5, msgsclr=5, headclr=4, taskclr=5\n")
topfile.write("Usr fieldscur=ABDECGfhijlopqrstuvyzMKNWX\n")
topfile.write(" winflags=62777, sortindx=4, maxtasks=0\n")
topfile.write(" summclr=3, msgsclr=3, headclr=2, taskclr=3\n")
topdata = run_shell_cmd("top -bH -n1")
if data != [""]:
with open("/root/.toprc", "w") as topfile:
topfile.write(''.join(data))
else:
os.remove("/root/.toprc")
return topdata
def run_sql_cmd(cmd):
# Supports mysql, postgresql, mssql, oracle and sqlite.
# Engine Format: engine://user:password@ip:port/database
# Read for more info: http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html
engine = sqlalchemy.create_engine('')
# PostgreSQL: sql = text('SELECT * FROM pg_stat_activity');
# MySQL + oracle: sql = text('show full processlist;')
# MSSQL: sql = text('sp_who2;')
# SQLite: lol what.
result = engine.execute('')
names = []
for row in result:
names.append(row[0])
print names
'''RHEL Related Functions'''
def detect_rhel_ver():
try:
with open("/etc/redhat-release", "r") as verfile:
# Make an array of any numbers (including floating points) in the string specified. - Regex explanation for Joe.
data = verfile.read.findall(r"[-+]?\d*\.*\d+")
# Since this is RHEL based, we only need the major version number.
return data[0]
except IOError:
print("Unable to open /etc/redhat-release.")
return False
return False
'''Script Releated Functions'''
def timestamp(): return time.strftime("%m_%d_%Y-%H_%M_%S")
def create_targz(name, file):
try:
with tarfile.open("/root/activityLog/" + name, "w:gz") as archive:
archive.add("/root/activityLog/" + file, arcname=file)
return True
except IOError:
print("Unable to save into activityLog archive.")
return False
return False
def alert_qn_support():
msg = MIMEMultipart()
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"
msg['Subject'] = "Activity Log Report - "
body = "Python Test Email"
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
def setup():
if os.getlogin() != "root":
print("You need to be root to run this script.")
exit(-2)
if root_fs_mountable and test_file_writable:
if not os.path.exists("/root/activityLog"): os.makedirs("/root/activityLog")
def main():
parser=argparse.ArgumentParser()
#parser.add_argument('foo', nargs='+')
#if len(sys.argv)==1:
# parser.print_help()
# sys.exit(1)
args=parser.parse_args()
setup()
hostname = get_hostname()
#rhel_version = detect_rhel_ver()
topdata = get_top_data()
loadavgs = get_loadavgs()
netbandwidth = run_shell_cmd("netstat -i")
netdaemons = run_shell_cmd("netstat -plunt")
netconnect = run_shell_cmd("netstat -W")
# sqldiag = get_sql_data();
'''Time to write everything to file.'''
filename = timestamp() + ".log"
with open("/root/activityLog/" + filename, "w") as activityLog:
activityLog.write(''.join(topdata))
activityLog.write("\n\n\n\n\n\n\n")
activityLog.write("Throughput on Network Interfaces:\n")
activityLog.write(''.join(netbandwidth))
activityLog.write("\n\n")
activityLog.write("Daemons and Open Ports List:\n")
activityLog.write(''.join(netdaemons))
activityLog.write("\n\n")
activityLog.write("Network Connections:\n")
activityLog.write(''.join(netconnect))
activityLog.write("\n\n\n\n\n")
activityLog.write("SQL Queries Active at " + timestamp())
create_targz("activityLog.tar.gz", filename)
os.remove("/root/activityLog/" + filename)
# Email Support
# alert_support()
if __name__ == "__main__": main()