-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.py
136 lines (110 loc) · 4.48 KB
/
common.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
# -*- coding: utf-8 -*-
import re
import psutil
from amplify.agent.collectors.abstract import AbstractCollector
from amplify.agent.common.context import context
from amplify.agent.common.util import subp
from amplify.agent.objects.nginx.binary import nginx_v
__author__ = "Mike Belov"
__copyright__ = "Copyright (C) Nginx, Inc. All rights reserved."
__credits__ = ["Mike Belov", "Andrei Belov", "Ivan Poluyanov", "Oleg Mamontov", "Andrew Alexeev", "Grant Hulegaard"]
__license__ = ""
__maintainer__ = "Mike Belov"
__email__ = "dedm@nginx.com"
class NginxCommonMetaCollector(AbstractCollector):
short_name = 'common_nginx_meta'
def collect(self):
meta = {
'type': 'nginx', # Hard coded since only 1 'nginx' object in backend.
'local_id': self.object.local_id,
'root_uuid': None,
'running': True,
'stub_status_enabled': self.object.stub_status_enabled,
'status_module_enabled': self.object.plus_status_enabled,
'stub_status_url': self.object.stub_status_url,
'plus_status_url': self.object.plus_status_external_url or self.object.plus_status_internal_url,
'version': None,
'plus': {
'enabled': False,
'release': None
},
'configure': {},
'packages': {},
'path': {
'bin': self.object.bin_path,
'conf': self.object.conf_path,
},
'warnings': [],
'ssl': {},
}
for method in (
self.root_uuid,
self.nginx_minus_v,
self.find_packages,
self.open_ssl
):
try:
method(meta)
except Exception as e:
exception_name = e.__class__.__name__
context.log.error('failed to collect meta %s due to %s' % (method.__name__, exception_name))
context.log.debug('additional info:', exc_info=True)
return meta
def root_uuid(self, meta):
meta['root_uuid'] = self.object.root_uuid or context.objects.root_object.uuid
def nginx_minus_v(self, meta):
""" call -V and parse """
parsed_v = nginx_v(self.object.bin_path)
meta['ssl'] = parsed_v['ssl']
meta['version'] = parsed_v['version']
meta['plus'] = parsed_v['plus']
meta['configure'] = parsed_v['configure']
def find_packages(self, meta):
pass
def open_ssl(self, meta):
"""Old nginx uses standart openssl library - find its version"""
if not meta['ssl']:
openssl_out, _ = subp.call("dpkg -l | grep openssl")
for line in openssl_out:
gwe = re.match('([\d\w]+)\s+([\d\w\.\-]+)\s+([\d\w\.\-\+_~]+)\s', line)
if gwe:
if gwe.group(2).startswith('openssl'):
meta['ssl'] = {
'built': [gwe.group(2), gwe.group(3)],
'run': [gwe.group(2), gwe.group(3)],
}
class NginxMetaCollector(NginxCommonMetaCollector):
short_name = 'nginx_meta'
def collect(self):
meta = super(NginxMetaCollector, self).collect()
meta.update({
'parent_hostname': context.hostname,
'pid': self.object.pid,
'start_time': None
})
for method in (
self.nginx_uptime,
):
try:
method(meta)
except Exception as e:
exception_name = e.__class__.__name__
context.log.error('failed to collect meta %s due to %s' % (method.__name__, exception_name))
context.log.debug('additional info:', exc_info=True)
self.object.metad.meta(meta)
def nginx_uptime(self, meta):
# collect info about start time
master_process = psutil.Process(self.object.pid)
meta['start_time'] = int(master_process.create_time()) * 1000
class ContainerNginxMetaCollector(NginxCommonMetaCollector):
short_name = 'container_nginx_meta'
def collect(self):
"""
Since Docker meta collects a subset of the regular Nginx object, all we have to do is call the common collect
method and put the meta payload on the MetadClient.
"""
meta = super(ContainerNginxMetaCollector, self).collect()
meta.update({
'parent_hostname': context.app_config['credentials']['imagename']
})
self.object.metad.meta(meta)