Форум сайта python.su
0
Всем салют!
Возникла следующая задача: необходимо создать конфиг файл nagios по определенному шаблону. Например:
у шаблона такой вид:
define service{ use generic-service ; Name of service template to use host_name @HOST@ service_description @SERVICE@ is_volatile 0 check_period 24x7 max_check_attempts 4 normal_check_interval 1 retry_check_interval 1 contact_groups users notification_options w,u,c,r notification_interval 960 notification_period 24x7 check_command check_@service@ }
define service{ use generic-service ; Name of service template to use host_name serv1 service_description tcp 53 is_volatile 0 check_period 24x7 max_check_attempts 4 normal_check_interval 1 retry_check_interval 1 contact_groups users notification_options w,u,c,r notification_interval 960 notification_period 24x7 check_command check_tcp!53 } define service{ use generic-service ; Name of service template to use host_name serv1 service_description tcp 20000 is_volatile 0 check_period 24x7 max_check_attempts 4 normal_check_interval 1 retry_check_interval 1 contact_groups users notification_options w,u,c,r notification_interval 960 notification_period 24x7 check_command check_tcp!20000 }
Офлайн
568
template = "define service{{\ use generic-service\n\ host_name {0}\n\ service_description {1}\n\ is_volatile 0\n\ check_period 24x7\n\ max_check_attempts 4\n\ normal_check_interval 1\n\ retry_check_interval 1\n\ contact_groups users\n\ notification_options w,u,c,r\n\ notification_interval 960\n\ notification_period 24x7\n\ check_command check_{1}\n\ }}" def get_service(host, service): return template.format(host, service) result = [] with open('settings', 'r') as f: host = service = "" for s in f: if s.startswith("serv"): host = s service = "" else: service = s.strip() if host and service: result.append(get_service(host, service)) result = "\n".join(result) print result
Офлайн
0
FishHookСпасибо, это решение подходит.
Офлайн