-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzen_retrieve.py
163 lines (128 loc) · 4.73 KB
/
zen_retrieve.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
#############################
import sys
import datetime
import os
import subprocess
#############################
class ConfigCollector(object):
""" """
"""
#------------------------------------------------------------------------------------
# Class variables
#------------------------------------------------------------------------------------
"""
_OUTPUT_ROOT = '/tmp/'
_OUTPUT_REPORT_NAME = 'zen_retrieve_' # the report name has a timestamp appended
_ZOPE_CONFIG_FILE = '/etc/zope.conf'
_MEMCACHE_CONFIG_FILE = '/etc/sysconfig/memcached'
def __init__(self, zen_home):
""" """
self._zenhome = zen_home
self._output_file = ''
@staticmethod
def get_timestamp():
""" """
now = datetime.datetime.now()
timestamp = now.strftime("%Y%m%d_%H%M%S")
return timestamp
def _initialize_report_name(self):
""" """
self._output_file = '{0}{1}{2}.txt'.format(self._OUTPUT_ROOT, self._OUTPUT_REPORT_NAME, ConfigCollector.get_timestamp())
def _get_config(self):
""" """
self._get_memory_info()
self._get_zope_conf()
self._get_catalog_size()
self._get_memcache_conf()
self._run_zentune()
def get_configuration(self):
""" """
print '\nRetrieving information... please wait.'
self._initialize_report_name()
self._get_config()
print '\nConfiguration retrieved to file {0} !!'.format(self._output_file)
"""
#------------------------------------------------------------------------------------
# Method that executes Unix commands
#------------------------------------------------------------------------------------
"""
def _execute_command(self, command):
"""
Params: command to execute
Return: tuple containing the stout and stderr of the command execution
"""
#print 'Executing ....' + command
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
return (stdout, stderr)
"""
#------------------------------------------------------------------------------------
# Method that writes info to the output file
#------------------------------------------------------------------------------------
"""
def _write_to_output(self, title, command, out, err):
""" """
f_report = open(self._output_file, 'a')
# Writes header
f_report.write('#'*60 + "\n")
f_report.write('# {0}\n'.format(title))
f_report.write('# Command: {0}\n'.format(command))
f_report.write('#'*60 + "\n\n")
# Writes stdout
f_report.write('-'*60 + "\n")
f_report.write(" STDOUT\n")
f_report.write('-'*60 + "\n")
f_report.write("{0}\n".format(str(out)))
# Writes stderr
f_report.write('-'*60 + "\n")
f_report.write(" STDERR\n")
f_report.write('-'*60 + "\n")
f_report.write("{0}\n".format(str(err)))
f_report.close()
"""
#------------------------------------------------------------------------------------
# Methods to retrieve the info
#------------------------------------------------------------------------------------
"""
def _get_memory_info(self):
""" """
command = 'cat /proc/meminfo'
(out, err) = self._execute_command(command)
self._write_to_output("", command, out, err)
def _get_zope_conf(self):
""" """
command = 'cat {0}/{1}'.format(self._zenhome, ConfigCollector._ZOPE_CONFIG_FILE)
(out, err) = self._execute_command(command)
self._write_to_output("", command, out, err)
def _get_catalog_size(self):
""" """
zendmd = '{0}/bin/zendmd'.format(self._zenhome)
command = '{0} << EOF\ndmd.global_catalog.__len__()\nquit()\nEOF'.format(zendmd)
(out, err) = self._execute_command(command)
self._write_to_output("", command, out, err)
def _get_memcache_conf(self):
""" """
command = 'cat {0}'.format(ConfigCollector._MEMCACHE_CONFIG_FILE)
(out, err) = self._execute_command(command)
self._write_to_output("", command, out, err)
def _run_zentune(self):
""" """
zentune = '{0}/bin/zentune'.format(self._zenhome)
command = '{0} run'.format(zentune)
(out, err) = self._execute_command(command)
self._write_to_output("", command, out, err)
"""
#------------------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------------------
"""
if __name__ == '__main__':
zen_home = os.getenv('ZENHOME', '/opt/zenoss')
if os.path.isdir(zen_home):
config_collector = ConfigCollector(zen_home)
config_collector.get_configuration()
else:
print '\n\nERROR: Enviroment variable ZENHOME is not defined! \n\n'
"""
#------------------------------------------------------------------------------------
"""