3434from azurelinuxagent .ga import logcollector , cgroupconfigurator
3535from azurelinuxagent .ga .cgroupcontroller import AGENT_LOG_COLLECTOR
3636from azurelinuxagent .ga .cpucontroller import _CpuController
37- from azurelinuxagent .ga .cgroupapi import get_cgroup_api , log_cgroup_warning , InvalidCgroupMountpointException
37+ from azurelinuxagent .ga .cgroupapi import create_cgroup_api , InvalidCgroupMountpointException
38+ from azurelinuxagent .ga .firewall_manager import FirewallManager
3839
3940import azurelinuxagent .common .conf as conf
4041import azurelinuxagent .common .event as event
4142import azurelinuxagent .common .logger as logger
43+ from azurelinuxagent .common .event import WALAEventOperation
4244from azurelinuxagent .common .future import ustr
4345from azurelinuxagent .ga .logcollector import LogCollector , OUTPUT_RESULTS_FILE_PATH
4446from azurelinuxagent .common .osutil import get_osutil
4547from azurelinuxagent .common .utils import fileutil , textutil
4648from azurelinuxagent .common .utils .flexible_version import FlexibleVersion
47- from azurelinuxagent .common .utils .networkutil import AddFirewallRules
4849from azurelinuxagent .common .version import AGENT_NAME , AGENT_LONG_VERSION , AGENT_VERSION , \
4950 DISTRO_NAME , DISTRO_VERSION , \
5051 PY_VERSION_MAJOR , PY_VERSION_MINOR , \
@@ -208,28 +209,34 @@ def collect_logs(self, is_full_mode):
208209 else :
209210 logger .info ("Running log collector mode normal" )
210211
212+ LogCollector .initialize_telemetry ()
213+
211214 # Check the cgroups unit
212215 log_collector_monitor = None
213216 tracked_controllers = []
214217 if CollectLogsHandler .is_enabled_monitor_cgroups_check ():
215218 try :
216- cgroup_api = get_cgroup_api ()
219+ cgroup_api = create_cgroup_api ()
220+ logger .info ("Using cgroup {0} for resource enforcement and monitoring" .format (cgroup_api .get_cgroup_version ()))
217221 except InvalidCgroupMountpointException as e :
218- log_cgroup_warning ( "The agent does not support cgroups if the default systemd mountpoint is not being used: {0}" . format ( ustr (e )), send_event = True )
222+ event . warn ( WALAEventOperation . LogCollection , "The agent does not support cgroups if the default systemd mountpoint is not being used: {0}" , ustr (e ))
219223 sys .exit (logcollector .INVALID_CGROUPS_ERRCODE )
220224 except CGroupsException as e :
221- log_cgroup_warning ( "Unable to determine which cgroup version to use: {0}" . format ( ustr (e )), send_event = True )
225+ event . warn ( WALAEventOperation . LogCollection , "Unable to determine which cgroup version to use: {0}" , ustr (e ))
222226 sys .exit (logcollector .INVALID_CGROUPS_ERRCODE )
223227
224228 log_collector_cgroup = cgroup_api .get_process_cgroup (process_id = "self" , cgroup_name = AGENT_LOG_COLLECTOR )
225229 tracked_controllers = log_collector_cgroup .get_controllers ()
230+ for controller in tracked_controllers :
231+ logger .info ("{0} controller for cgroup: {1}" .format (controller .get_controller_type (), controller ))
226232
227233 if len (tracked_controllers ) != len (log_collector_cgroup .get_supported_controller_names ()):
228- log_cgroup_warning ( "At least one required controller is missing. The following controllers are required for the log collector to run: {0}" . format ( log_collector_cgroup .get_supported_controller_names () ))
234+ event . warn ( WALAEventOperation . LogCollection , "At least one required controller is missing. The following controllers are required for the log collector to run: {0}" , log_collector_cgroup .get_supported_controller_names ())
229235 sys .exit (logcollector .INVALID_CGROUPS_ERRCODE )
230236
231- if not log_collector_cgroup .check_in_expected_slice (cgroupconfigurator .LOGCOLLECTOR_SLICE ):
232- log_cgroup_warning ("The Log Collector process is not in the proper cgroups" , send_event = False )
237+ expected_slice = cgroupconfigurator .LOGCOLLECTOR_SLICE
238+ if not log_collector_cgroup .check_in_expected_slice (expected_slice ):
239+ event .warn (WALAEventOperation .LogCollection , "The Log Collector process is not in the proper cgroups. Expected slice: {0}" , expected_slice )
233240 sys .exit (logcollector .INVALID_CGROUPS_ERRCODE )
234241
235242 try :
@@ -270,15 +277,16 @@ def collect_logs(self, is_full_mode):
270277 log_collector_monitor .stop ()
271278
272279 @staticmethod
273- def setup_firewall (firewall_metadata ):
274-
275- print ("Setting up firewall for the WALinux Agent with args: {0}" .format (firewall_metadata ))
280+ def setup_firewall (endpoint ):
281+ logger .set_prefix ("Firewall" )
282+ threading .current_thread ().name = "Firewall"
283+ event .info (event .WALAEventOperation .Firewall , "Setting up firewall after boot. Endpoint: {0}" , ustr (endpoint ))
276284 try :
277- AddFirewallRules . add_iptables_rules ( firewall_metadata [ 'wait' ], firewall_metadata [ 'dst_ip' ],
278- firewall_metadata [ 'uid' ] )
279- print ( "Successfully set the firewall rules" )
285+ firewall_manager = FirewallManager . create ( endpoint )
286+ firewall_manager . setup ( )
287+ event . info ( event . WALAEventOperation . Firewall , "Successfully set the firewall rules" )
280288 except Exception as error :
281- print ( "Unable to add firewall rules. Error: {0}" . format ( ustr (error ) ))
289+ event . error ( event . WALAEventOperation . Firewall , "Unable to add firewall rules. Error: {0}" , ustr (error ))
282290 sys .exit (1 )
283291
284292
@@ -291,7 +299,7 @@ def main(args=None):
291299 args = []
292300 if len (args ) <= 0 :
293301 args = sys .argv [1 :]
294- command , force , verbose , debug , conf_file_path , log_collector_full_mode , firewall_metadata = parse_args (args )
302+ command , force , verbose , debug , conf_file_path , log_collector_full_mode , firewall_endpoint = parse_args (args )
295303 if command == AgentCommands .Version :
296304 version ()
297305 elif command == AgentCommands .Help :
@@ -318,7 +326,7 @@ def main(args=None):
318326 elif command == AgentCommands .CollectLogs :
319327 agent .collect_logs (log_collector_full_mode )
320328 elif command == AgentCommands .SetupFirewall :
321- agent .setup_firewall (firewall_metadata )
329+ agent .setup_firewall (firewall_endpoint )
322330 except Exception as e :
323331 logger .error (u"Failed to run '{0}': {1}" ,
324332 command ,
@@ -335,11 +343,7 @@ def parse_args(sys_args):
335343 debug = False
336344 conf_file_path = None
337345 log_collector_full_mode = False
338- firewall_metadata = {
339- "dst_ip" : None ,
340- "uid" : None ,
341- "wait" : ""
342- }
346+ endpoint = None
343347
344348 regex_cmd_format = "^([-/]*){0}"
345349
@@ -383,20 +387,17 @@ def parse_args(sys_args):
383387 cmd = AgentCommands .CollectLogs
384388 elif re .match (regex_cmd_format .format ("full" ), arg ):
385389 log_collector_full_mode = True
386- elif re .match (regex_cmd_format .format (AgentCommands .SetupFirewall ), arg ):
387- cmd = AgentCommands .SetupFirewall
388- elif re .match (regex_cmd_format .format ("dst_ip=(?P<dst_ip>[\\ d.]{7,})" ), arg ):
389- firewall_metadata ['dst_ip' ] = re .match (regex_cmd_format .format ("dst_ip=(?P<dst_ip>[\\ d.]{7,})" ), arg ).group (
390- 'dst_ip' )
391- elif re .match (regex_cmd_format .format ("uid=(?P<uid>[\\ d]+)" ), arg ):
392- firewall_metadata ['uid' ] = re .match (regex_cmd_format .format ("uid=(?P<uid>[\\ d]+)" ), arg ).group ('uid' )
393- elif re .match (regex_cmd_format .format ("(w|wait)$" ), arg ):
394- firewall_metadata ['wait' ] = "-w"
395390 else :
396- cmd = AgentCommands .Help
397- break
391+ regex_cmd = regex_cmd_format .format ("{0}=(?P<endpoint>[\\ d.]{{7,}})" .format (AgentCommands .SetupFirewall ))
392+ match = re .match (regex_cmd , arg )
393+ if match is not None :
394+ cmd = AgentCommands .SetupFirewall
395+ endpoint = match .group ('endpoint' )
396+ else :
397+ cmd = AgentCommands .Help
398+ break
398399
399- return cmd , force , verbose , debug , conf_file_path , log_collector_full_mode , firewall_metadata
400+ return cmd , force , verbose , debug , conf_file_path , log_collector_full_mode , endpoint
400401
401402
402403def version ():
@@ -416,11 +417,11 @@ def usage():
416417 """
417418 Return agent usage message
418419 """
419- s = "\n "
420+ s = "\n "
420421 s += ("usage: {0} [-verbose] [-force] [-help] "
421422 "-configuration-path:<path to configuration file>"
422423 "-deprovision[+user]|-register-service|-version|-daemon|-start|"
423- "-run-exthandlers|-show-configuration|-collect-logs [-full]|-setup-firewall [-dst_ip =<IP> -uid=<UID> [-w/--wait] ]"
424+ "-run-exthandlers|-show-configuration|-collect-logs [-full]|-setup-firewall=<IP>]"
424425 "" ).format (sys .argv [0 ])
425426 s += "\n "
426427 return s
0 commit comments