Skip to content

Instantly share code, notes, and snippets.

@rfairburn
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfairburn/00dcaf60c4748aa9d351 to your computer and use it in GitHub Desktop.
Save rfairburn/00dcaf60c4748aa9d351 to your computer and use it in GitHub Desktop.
#!/bin/bash
# shell script to convert a nagios .cfg file to yaml compatible with my nagios formula.
FILENAME=$1
DESTINATION=$2
GENERATED_FILENAME=$3
OBJECTTYPES=$(cat ${FILENAME} | awk '{ if ($1 == "define") { print $2 }}' | sed 's/{//' | sort -u)
awkit() {
FILENAME=$1
DESTINATION=$2
OBJECTTYPE=$3
awk '
/define/,/}/ {
if ($2 ~ "'${OBJECTTYPE}'($|{)") {
itemindex=0
objectname = ""
delete itemrow
getline
while ($1 !~ "}") {
if ($1 ~ "^($|#|;)") {
getline
} else {
# print "DEBUG FULL LINE IS :"$0
if ($1 ~ "^('${OBJECTTYPE}'_)?name") {
objectname=" "$2":"
} else if (objectname ~ "^$" && $1 ~ "'${OBJECTTYPE}'_description") {
objectname=" "
for (i=2;i<=NF;i++) {
if ($i ~ "^;") {
i = NF
} else {
objectname=objectname "" $i
}
}
objectname=objectname ":"
}
itemname=$1
itemcontents = ""
for (i=2;i<=NF;i++) {
if ($i ~ "^;") {
i = NF
} else {
itemcontents=itemcontents "" $i
if (i != NF) {
itemcontents=itemcontents "" OFS
}
# print "DEBUG itemcontents: "itemcontents
}
}
itemrow[itemindex]=" - "itemname": '\''"itemcontents"'\''"
itemindex++
getline
}
if ($1 ~ "}") {
# print "DEBUG: "objectname
print objectname
for (i=0;i<length(itemrow);i++) {
print itemrow[i]
}
itemindex=0
}
}
}
}
' ${FILENAME} >> ${DESTINATION}
}
echo ${GENERATED_FILENAME}: > ${DESTINATION}
for OBJECTTYPE in ${OBJECTTYPES}; do
echo " ${OBJECTTYPE}:" >> ${DESTINATION}
awkit ${FILENAME} ${DESTINATION} ${OBJECTTYPE}
done
#!/bin/bash
# Take the nrpe configs in /etc/nrpe.d and convert them for yaml to be directly pasted in the pillar under the following:
#
# nrpe:
# config:
# <whatever configs>
# <whatever other options such as "use_default_autocheck_template: True">
# additional_configs:
# <paste starts on this line>
CONFIGFILE=$1
OUTPUTFILE=$2
GENERATEDFILENAME=$3
awkit() {
local CONFIGFILE=$1
local OUTPUTFILE=$2
local GENERATEDFILENAME=$3
awk '
BEGIN {
FS="="
print " '${GENERATEDFILENAME}':"
hascommands = "False"
hasoptions = "False"
commands = ""
options = ""
}
! /^\s*(#|$)/ {
if ($1 ~ "command") {
hascommands = "True"
commands = commands " " substr($1,9,length($1)-9) ": \"" $2 "\"\n"
} else {
hasoptions = "True"
options = options " " $1 ": \"" $2 "\"\n"
}
}
END {
if (hascommands == "True") {
print " commands:"
printf commands
}
if (hasoptions == "True") {
print " options:"
printf options
}
}
' ${CONFIGFILE} >> ${OUTPUTFILE}
}
awkit "${CONFIGFILE}" "${OUTPUTFILE}" "${GENERATEDFILENAME}"
# Or for all of /etc/nrpe.d:
# for i in /etc/nrpe.d/*.cfg; do awkit $i ${OUTPUTFILE) /etc/nrpe.d/${i##*/}; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment