MyNixOS website logo
Description

EDSL to specify Nagios configuration files.

EDSL to specify Nagios configuration files. Encode nagios configuration using Haskell types.

nagios-conf

EDSL to specify Nagios configuration files.

Example

#!/usr/bin/env stack
{- stack --resolver lts-9.21 --package nagios-conf script -}
module Main where

import Nagios.Config.EDSL
import Nagios.Config.EDSL.Defaults (linuxServer)
import Nagios.Config.EDSL.Defaults.Commands
import qualified Nagios.Config.EDSL.Defaults as Defaults

import Data.List (isPrefixOf)

main :: IO ()
main = putStr $ writeConfiguration toplevel

toplevel :: [Object]
toplevel = map OService
    [ ping
    , sshLoad, sshDisk, sshUsers, sshProcesses, sshSwap
    , sshLxdDisk, sshRaid
    , sshIomegaDisk
    ] ++ map OHostGroup
    [ allHosts
    , linuxSshServers
    , lxdContainers
    , lxdHosts
    , raidServers
    ]

-- --------------------------------
-- ---------- HOSTGROUPS ----------
-- --------------------------------
allHosts :: HostGroup
allHosts = (hostgroup "all-servers" "All Servers")
    { hostGroupMembers = hostGroupMembers linuxSshServers ++
                         hostGroupMembers lxdContainers}

linuxSshServers :: HostGroup
linuxSshServers = (hostgroup "linux-ssh-servers" "Linux SSH Servers")
    { hostGroupMembers = [xen, admin, dmz, nfs, lxd, lxd2] }

lxdHosts :: HostGroup
lxdHosts = (hostgroup "lxd-hosts" "LXD Hosts")
    { hostGroupMembers = [lxd, lxd2] }

raidServers :: HostGroup
raidServers = (hostgroup "raid-servers" "RAID Servers")
    { hostGroupMembers = [xen, lxd2] }

lxdContainers :: HostGroup
lxdContainers = (hostgroup "lxd-containers" "LXD Containers")
    { hostGroupMembers = [ backups
                         , henri
                         , sniproxy
                         , unigornelJenkins
                         , gitea, giteaCaddy
                         , jenkins, jenkinsCaddy
                         , nagios, nagiosCaddy
                         , openvpn
                         ]
    }

-- ------------------------------
-- ---------- SERVICES ----------
-- ------------------------------
ping :: Service
ping = (service "ping")
    { serviceUse = Just localService
    , serviceDescription = Just "PING"
    , serviceHostGroups = [allHosts]
    , serviceCheckCommand = Just $ apply checkPing ["100.0,20%", "500.0,60%"]
    }

sshLoad :: Service
sshLoad = (service "ssh-load")
    { serviceUse = Just localService
    , serviceDescription = Just "Load"
    , serviceHostGroups = [linuxSshServers]
    , serviceCheckCommand = Just $ apply checkSshLoad ["5.0,4.0,3.0", "10.0,6.0,4.0"]
    }

sshDisk :: Service
sshDisk = (service "ssh-disk")
    { serviceUse = Just localService
    , serviceDescription = Just "Disk"
    , serviceHostGroups = [linuxSshServers]
    , serviceCheckCommand = Just $ apply checkSshDisk ["20%", "10%", "/"]
    }

sshUsers :: Service
sshUsers = (service "ssh-users")
    { serviceUse = Just localService
    , serviceDescription = Just "Users"
    , serviceHostGroups = [linuxSshServers]
    , serviceCheckCommand = Just $ apply checkSshUsers ["1", "5"]
    }

sshProcesses :: Service
sshProcesses = (service "ssh-processes")
    { serviceUse = Just localService
    , serviceDescription = Just "Processes"
    , serviceHostGroups = [linuxSshServers]
    , serviceCheckCommand = Just $ apply checkSshProcesses ["240", "400", "RSZDT"]
    }

sshSwap :: Service
sshSwap = (service "ssh-swap")
    { serviceUse = Just localService
    , serviceDescription = Just "Swap"
    , serviceHostGroups = [linuxSshServers]
    , serviceCheckCommand = Just $ apply checkSshSwap ["20", "10"]
    }

sshLxdDisk :: Service
sshLxdDisk = (service "ssh-lxd-disk")
    { serviceUse = Just localService
    , serviceDescription = Just "LXD Disk"
    , serviceHostGroups = [lxdHosts]
    , serviceCheckCommand = Just $ apply checkSshDisk ["20%", "10%", "/var/lib/lxd"]
    }

sshRaid :: Service
sshRaid = (service "ssh-raid")
    { serviceUse = Just localService
    , serviceDescription = Just "RAID"
    , serviceHostGroups = [raidServers]
    , serviceCheckCommand = Just checkSshRaid
    }

sshIomegaDisk :: Service
sshIomegaDisk = (service "ssh-iomega-disk")
    { serviceUse = Just localService
    , serviceDescription = Just "Iomega Disk"
    , serviceHosts = [nfs]
    , serviceCheckCommand = Just $ apply checkSshDisk ["20%", "10%", "/mnt/iomega"]
    }

-- ---------------------------
-- ---------- HOSTS ----------
-- ---------------------------
lanHost :: String -> Host
lanHost name = (host name)
    { hostUse = Just linuxServer
    , hostHostName = Just name
    , hostAlias = Just name
    , hostAddress = Just name
    }

xen :: Host
xen = lanHost "xen"

admin :: Host
admin = lanHost "admin"

dmz :: Host
dmz = lanHost "dmz"

nfs :: Host
nfs = lanHost "nfs"

lxd :: Host
lxd = lanHost "lxd"

lxd2 :: Host
lxd2 = lanHost "lxd2"

backups :: Host
backups = lanHost "backups"

henri :: Host
henri = lanHost "henri"

sniproxy :: Host
sniproxy = lanHost "sniproxy"

unigornelJenkins :: Host
unigornelJenkins = lanHost "unigornel-jenkins"

gitea :: Host
gitea = lanHost "gitea"

giteaCaddy :: Host
giteaCaddy = lanHost "gitea-caddy"

jenkins :: Host
jenkins = lanHost "jenkins"

jenkinsCaddy :: Host
jenkinsCaddy = lanHost "jenkins-caddy"

nagios :: Host
nagios = lanHost "nagios"

nagiosCaddy :: Host
nagiosCaddy = lanHost "nagios-caddy"

openvpn :: Host
openvpn = lanHost "openvpn"

-- ------------------------------
-- ---------- COMMANDS ----------
-- ------------------------------
newSshCommand :: String -> String -> Command
newSshCommand name utility =
    Command name $
            "$USER1$/check_by_ssh -H $HOSTADDRESS$ -l nagios -C \"" ++ utility' ++ "\""
  where
    utility' = replace "\"" "\\\"" utility

checkSshDisk :: Command
checkSshDisk = newSshCommand "check-ssh-disk"
                             "$USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$"

checkSshLoad :: Command
checkSshLoad = newSshCommand "check-ssh-load"
                             "$USER1$/check_load -w $ARG1$ -c $ARG2$"

checkSshUsers :: Command
checkSshUsers = newSshCommand "check-ssh-users"
                              "$USER1$/check_users -w $ARG1$ -c $ARG2$"

checkSshProcesses :: Command
checkSshProcesses = newSshCommand "check-ssh-processes"
                                  "$USER1$/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$"

checkSshSwap :: Command
checkSshSwap = newSshCommand "check-ssh-swap"
                             "$USER1$/check_swap -w $ARG1$ -c $ARG2$"

checkSshRaid :: CommandApp
checkSshRaid = flip apply [] $
               newSshCommand "check-ssh-raid"
                             "$USER1$/check_md_raid"

replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace a b = replace' []
  where
    replace' l [] = l
    replace' l c@(x:xs) | a `isPrefixOf` c = replace' (l ++ b) (drop (length a) c)
                        | otherwise = replace' (l ++ [x]) xs

-- -----------------------------------
-- ---------- REDEFINITIONS ----------
-- -----------------------------------
localService :: Service
localService = Defaults.localService
    { serviceUse = Just genericService }

genericService :: Service
genericService = Defaults.genericService
    { serviceContactGroups = [admins] }

genericContact :: Contact
genericContact = Defaults.genericContact
    { contactServiceNotificationCommands = Just notifyServiceByEmail
    , contactHostNotificationCommands = Just notifyHostByEmail }

nagiosadmin :: Contact
nagiosadmin = (contact "nagiosadmin")
    { contactUse = Just genericContact
    , contactAlias = Just "Nagios Admin"
    , contactEmail = Just "[email protected]"
    }

admins :: ContactGroup
admins = (contactgroup "admins" "Nagios Administrators")
    { contactGroupMembers = [nagiosadmin] }

notifyHostByEmail :: CommandApp
notifyHostByEmail = flip apply [] $
                    Command "notify-host-by-email"
                            "/usr/bin/printf \"%b\" \"***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\nHost: $HOSTNAME$\\nState: $HOSTSTATE$\\nAddress: $HOSTADDRESS$\\nInfo: $HOSTOUTPUT$\\n\\nDate/Time: $LONGDATETIME$\\n\" | $USER1$/go-smtp-send -subject \"** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **\" -to $CONTACTEMAIL$"

notifyServiceByEmail :: CommandApp
notifyServiceByEmail = flip apply [] $
                       Command "notify-service-by-email"
                               "/usr/bin/printf \"%b\" \"***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\n\\nService: $SERVICEDESC$\\nHost: $HOSTALIAS$\\nAddress: $HOSTADDRESS$\\nState: $SERVICESTATE$\\n\\nDate/Time: $LONGDATETIME$\\n\\nAdditional Info:\\n\\n$SERVICEOUTPUT$\\n\" | $USER1$/go-smtp-send -subject \"** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **\" -to $CONTACTEMAIL$"
Metadata

Version

0.1.0.0

Executables (1)

  • bin/nagios-config-example

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows