This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
gather_metrics.py
131 lines (109 loc) · 3.22 KB
/
gather_metrics.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
#!/usr/bin/env python
"""
The main module file that will be invoked by the one line installer.
Usage: gather_metrics [optional log file]
If log file is provided, then errors will be logged to such file.
Otherwise, all errors will be flushed.
It first checks the dependency this module needs.
Detects application and calls the appropriate plugin installer.
Catches ctrl+c to prevent long error message and exit the system
with return code of 1.
"""
import socket
import sys
import getopt
from datetime import datetime
import subprocess
import conf_collectd_plugin as conf
import install_utils as utils
import config
# Python required base version
REQ_VERSION = (2, 7)
def check_version():
cur_version = sys.version_info
utils.print_step(' Checking python version')
if cur_version < REQ_VERSION:
sys.stderr.write(
'Your Python interpreter is older '
'than what we have tested, we suggest upgrading '
'to a newer 2.7 version before continuing.\n')
sys.exit()
utils.print_success()
def port_scan(host, port):
"""
Using ipv4, TCP connection to test whether a port is
being used.
"""
# AF_INET specifies ipv4, SOCK_STREAM for TCP
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
except socket.error:
return False
except KeyboardInterrupt:
print 'Scanning interrupted'
sys.exit()
except socket.gaierror:
print 'Hostname could not be resolved'
sys.exit()
else:
return port
finally:
sock.close()
def detect_used_ports():
"""
Scan through localhost 0-1024 ports
"""
MAX_PORT = 1025
DEFAULT_HOST = '127.0.0.1'
open_ports = []
socket.setdefaulttimeout(1)
for port in range(0, MAX_PORT):
res = port_scan(DEFAULT_HOST, port)
if res:
open_ports.append(port)
# debugging purpose to see if program is running
if port % 5000 == 0 and port != 0:
sys.stderr.write('.')
return open_ports
def detect_applications():
"""
Current collectd plugin support:
apache.
Want: nginx, sql, postgres, amcq
This function uses unix command ps -A and check whether
the supported application is listed.
"""
try:
res = subprocess.check_output(
'ps -A', shell=True,
stderr=subprocess.STDOUT,
executable='/bin/bash')
except:
print 'Unexpected error.'
sys.exit(1)
if 'apache' in res or 'httpd' in res:
conf.install_apache_plugin()
if 'nginx' in res:
print 'Has nginx'
if 'sql' in res:
print 'Has sql'
if 'postgres' in res:
print 'Has postgres'
if 'AMCQ' in res:
print 'Has AMCQ'
if __name__ == '__main__':
check_version()
conf.check_collectd_exists()
conf.check_collectd_path()
# the first argument will be the log file
if len(sys.argv) > 1:
config.INSTALL_LOG = sys.argv[1]
else:
config.INSTALL_LOG = '/dev/null'
try:
conf.print_log()
detect_applications()
except KeyboardInterrupt:
sys.stderr.write('\nQuitting the installer via keyboard interrupt.')
sys.exit(1)