Skip to content

Commit 3f5f1fe

Browse files
authored
Merge pull request #64 from plesk/CPCLOUD-2249-automon
[CPCLOUD-2249] Add automatic monitoring of hosted websites
2 parents 740d7b7 + a21b051 commit 3f5f1fe

File tree

1 file changed

+86
-3
lines changed

1 file changed

+86
-3
lines changed

agent360/agent360.py

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,33 @@ def info():
9494
def hello(proto='https'):
9595
parser = OptionParser()
9696
parser.add_option("-t", "--tags", help="Comma-separated list of tags")
97+
parser.add_option("-a", "--automon", type=int, default=0, help="Enable/disable automatic monitoring of hosted websites")
98+
9799
(options, args) = parser.parse_args()
98100

99101
user_id = args[0]
100102
agent = Agent(dry_instance=True)
103+
101104
if len(args) > 1:
102105
token_filename = args[1]
103106
else:
104107
token_filename = os.path.join(__FILEABSDIRNAME__, 'agent360-token.ini')
108+
105109
if len(args) > 2:
106110
unique_id = args[2]
107111
else:
108112
unique_id = ''
113+
109114
if options.tags is None:
110115
tags = ''
111116
else:
112117
tags = options.tags
118+
119+
if options.automon == 1:
120+
domains = ','.join(_get_domains())
121+
else:
122+
domains = ''
123+
113124
if '_' in user_id:
114125
server_id = user_id.split('_')[1]
115126
user_id = user_id.split('_')[0]
@@ -125,19 +136,89 @@ def hello(proto='https'):
125136
'hostname': hostname,
126137
'unique_id': unique_id,
127138
'tags': tags,
139+
'domains': domains,
128140
}).encode("utf-8")
129141
).read().decode()
142+
130143
if len(server_id) == 24:
131144
print('Got server_id: %s' % server_id)
132145
open(token_filename, 'w').\
133146
write('[DEFAULT]\nuser=%s\nserver=%s\n' % (user_id, server_id))
134147
else:
135148
print('Could not retrieve server_id: %s' % server_id)
136149

150+
def _get_apache_domains():
151+
domains = []
152+
153+
try:
154+
output = subprocess.check_output(['apachectl', '-S'])
155+
156+
for line in output.decode().splitlines():
157+
if 'namevhost' not in line:
158+
continue
159+
160+
cols = line.strip().split(' ')
161+
domains.append(cols[3])
162+
except FileNotFoundError:
163+
pass
164+
165+
return domains
166+
167+
def _get_nginx_domains():
168+
domains = []
169+
170+
try:
171+
output = subprocess.check_output(['nginx', '-T'])
172+
173+
for line in output.decode().splitlines():
174+
if 'server_name' not in line:
175+
continue
176+
177+
cols = line.strip().split(' ')
178+
179+
if len(cols) == 2:
180+
domain = cols[1].replace(';', '').replace('"', '')
181+
domains.append(domain)
182+
except FileNotFoundError:
183+
pass
184+
185+
return domains
137186

138-
# def run_agent():
139-
# Agent().run()
187+
def _get_domains():
188+
domains = []
189+
190+
try:
191+
json_str = subprocess.check_output(['whmapi1', '--output=jsonpretty', 'get_domain_info'])
192+
response = json.loads(json_str)
193+
194+
for domain in response['data']['domains']:
195+
domains.append(domain['domain'])
196+
except FileNotFoundError:
197+
try:
198+
str = subprocess.check_output(['plesk', 'bin', 'domain', '--list'])
140199

200+
for domain in str.decode().splitlines():
201+
domains.append(domain)
202+
except FileNotFoundError:
203+
for domain in list(set(_get_apache_domains() + _get_nginx_domains())):
204+
if '.' not in domain:
205+
continue
206+
207+
if domain.endswith('.localdomain'):
208+
continue
209+
210+
if domain.endswith('.localhost'):
211+
continue
212+
213+
if domain.endswith('.local'):
214+
continue
215+
216+
domains.append(domain)
217+
218+
return domains
219+
220+
def count_domains():
221+
print(len(_get_domains()))
141222

142223
def _plugin_name(plugin):
143224
if isinstance(plugin, basestring):
@@ -683,13 +764,15 @@ def main():
683764
elif sys.argv[1] == 'hello':
684765
del sys.argv[1]
685766
sys.exit(hello())
767+
elif sys.argv[1] == 'count-domains':
768+
del sys.argv[1]
769+
sys.exit(count_domains())
686770
elif sys.argv[1] == 'insecure-hello':
687771
del sys.argv[1]
688772
sys.exit(hello(proto='http'))
689773
elif sys.argv[1] == 'test':
690774
sys.exit(test_plugins(sys.argv[2:]))
691775
else:
692-
693776
print('Invalid option:', sys.argv[1], file=sys.stderr)
694777
sys.exit(1)
695778
else:

0 commit comments

Comments
 (0)