Найти - Пользователи
Полная версия: Python3 создание конфиг файла по шаблону
Начало » Python для новичков » Python3 создание конфиг файла по шаблону
1
GGnoob
Всем салют!
Возникла следующая задача: необходимо создать конфиг файл 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@
              }
также имеется файл со списком этих самых хостов и сервисов:
serv1
tcp 53
tcp 20000
tcp 35353

serv2
ping
tcp 30000

serv3
и т.д.
В общем на выходе нужен общий файл с соответствующими подстановками, т.е.,например:
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
              }
Но вообще check_command могут быть довольно разные, поэтому, скорее всего. их придется писать руками или делать еще шаблоны.
FishHook
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
GGnoob
FishHook
Спасибо, это решение подходит.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB