@@ -94,22 +94,33 @@ def info():
94
94
def hello (proto = 'https' ):
95
95
parser = OptionParser ()
96
96
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
+
97
99
(options , args ) = parser .parse_args ()
98
100
99
101
user_id = args [0 ]
100
102
agent = Agent (dry_instance = True )
103
+
101
104
if len (args ) > 1 :
102
105
token_filename = args [1 ]
103
106
else :
104
107
token_filename = os .path .join (__FILEABSDIRNAME__ , 'agent360-token.ini' )
108
+
105
109
if len (args ) > 2 :
106
110
unique_id = args [2 ]
107
111
else :
108
112
unique_id = ''
113
+
109
114
if options .tags is None :
110
115
tags = ''
111
116
else :
112
117
tags = options .tags
118
+
119
+ if options .automon == 1 :
120
+ domains = ',' .join (_get_domains ())
121
+ else :
122
+ domains = ''
123
+
113
124
if '_' in user_id :
114
125
server_id = user_id .split ('_' )[1 ]
115
126
user_id = user_id .split ('_' )[0 ]
@@ -125,19 +136,89 @@ def hello(proto='https'):
125
136
'hostname' : hostname ,
126
137
'unique_id' : unique_id ,
127
138
'tags' : tags ,
139
+ 'domains' : domains ,
128
140
}).encode ("utf-8" )
129
141
).read ().decode ()
142
+
130
143
if len (server_id ) == 24 :
131
144
print ('Got server_id: %s' % server_id )
132
145
open (token_filename , 'w' ).\
133
146
write ('[DEFAULT]\n user=%s\n server=%s\n ' % (user_id , server_id ))
134
147
else :
135
148
print ('Could not retrieve server_id: %s' % server_id )
136
149
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
137
186
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' ])
140
199
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 ()))
141
222
142
223
def _plugin_name (plugin ):
143
224
if isinstance (plugin , basestring ):
@@ -683,13 +764,15 @@ def main():
683
764
elif sys .argv [1 ] == 'hello' :
684
765
del sys .argv [1 ]
685
766
sys .exit (hello ())
767
+ elif sys .argv [1 ] == 'count-domains' :
768
+ del sys .argv [1 ]
769
+ sys .exit (count_domains ())
686
770
elif sys .argv [1 ] == 'insecure-hello' :
687
771
del sys .argv [1 ]
688
772
sys .exit (hello (proto = 'http' ))
689
773
elif sys .argv [1 ] == 'test' :
690
774
sys .exit (test_plugins (sys .argv [2 :]))
691
775
else :
692
-
693
776
print ('Invalid option:' , sys .argv [1 ], file = sys .stderr )
694
777
sys .exit (1 )
695
778
else :
0 commit comments