Я переношу некоторые старые данные конфигурации Nagios из Nagios Core в Nagios XI. Часть этой работы означает, что мне нужно извлечь некоторые определения объектов и поместить их в отдельные файлы, названные по имени хоста (пример ниже). Я вижу несколько способов сделать это, возможно, написав скрипт (Perl/Python/PHP - кажется, что все сценарии Nagios XI выполняются на PHP). Однако мне было интересно, есть ли более простой способ сделать это, возможно, в командной строке с использованием awk
или аналогичного? Меня поражает, что awk может достаточно легко извлекать строки текста между двумя шаблонами-разделителями (например, /define host \{/
и /\}/
), но мне нужно, чтобы вывод был разделен на отдельные файлы, названные по содержимому поля host_name.
Каков наилучший подход к этому? Мне лучше написать сценарий или есть удобная команда awk (или аналогичная), которую можно запустить из оболочки bash на машинах Nagios XI?
Пример монолитного файла:
define host {
host_name testhost1
use hosttemplate1
address 10.0.0.1
host_groups +linux,all
contact_groups +servicedesk
alias testhost1
icon_image redhat_icon.png
}
define service {
use servtemplate1
host_name testhost1
service_groups +All
service_description A Test Service
}
define host {
host_name testhost2
use hosttemplate2
address 10.0.0.2
host_groups +linux,all
contact_groups +servicedesk
alias testhost2
icon_image redhat_icon.png
}
Желаемый результат:
# cat testhost1.cfg
define host {
host_name testhost1
use hosttemplate1
address 10.0.0.1
host_groups +linux,all
contact_groups +servicedesk
alias testhost1
icon_image redhat_icon.png
}
# cat testhost2.cfg
define host {
host_name testhost2
use hosttemplate2
address 10.0.0.2
host_groups +linux,all
contact_groups +servicedesk
alias testhost2
icon_image redhat_icon.png
}
Теперь, например, я могу запустить такую команду, которая довольно широко используется для извлечения строки:
# gawk ' /define host / {flag=1;next} /}/{flag=0} flag { print }' example.cfg
Это обрезает define host
и }
, но это относительно легко исправить, однако данные выводятся как один поток в оболочке.
Есть ли какой-нибудь хитрый трюк, который я могу реализовать, чтобы сделать все это, включая разделение на отдельные файлы конфигурации из одного вкладыша в оболочке, или мне нужно написать сценарий?