PHP Script to parse nagios.dat

Please add your own error-handling:


function NagiosParser($file)
        $status = file_get_contents($file);

        $current = null;
        $parsed = array();

        foreach (explode("\n", $status) as $line)
                if (preg_match('/^([a-z]+) {$/', $line, $matches))              // Block start
                        $block = array();
                        $current = $matches[1];
                else if (preg_match('/}$/', $line, $matches))                   // Block stop
                        $parsed[$current][] = $block;
                        $current = null;
                else if (preg_match('/([a-z_]+)=(.+)/', $line, $matches))       // Not null value
                        if ($current == null)
                                die("Parse error: Value outside of block!");
                                $block[$matches[1]] = $matches[2];
        return $parsed;
Udgivet i Nagios, PHP | Skriv en kommentar

Nagios module for monitoring smsdlog for modem-connectivity


function secondsToTime($inputSeconds)
        $secondsInAMinute = 60;
        $secondsInAnHour  = 60 * $secondsInAMinute;
        $secondsInADay    = 24 * $secondsInAnHour;

        // extract days
        $days = floor($inputSeconds / $secondsInADay);

        // extract hours
        $hourSeconds = $inputSeconds % $secondsInADay;
        $hours = floor($hourSeconds / $secondsInAnHour);

        // extract minutes
        $minuteSeconds = $hourSeconds % $secondsInAnHour;
        $minutes = floor($minuteSeconds / $secondsInAMinute);

        // extract the remaining seconds
        $remainingSeconds = $minuteSeconds % $secondsInAMinute;
        $seconds = ceil($remainingSeconds);

        $res = array();

        if ($days > 0)
                $ret[] = "$days days";

        if ($hours > 0)
                $ret[] = "$hours hours";

        if ($minutes > 0)
                $ret[] = "$minutes minutes";

        if ($seconds > 0)
                $ret[] = "$seconds seconds";

        // return the final array
        $obj = array(
                'd' => (int) $days,
                'h' => (int) $hours,
                'm' => (int) $minutes,
                's' => (int) $seconds,
        if (empty($ret))
                return "0 seconds";
                return implode(", ",$ret);

$cmd = exec("grep 'Signal Strength' /var/log/smstools/smsd.log | /usr/bin/tail -n 1");
if (!$cmd)
        print("CRITICAL: No lifesign from modem in current logfile\n");

list ($year, $month, $day, $hour, $min, $sec) = preg_split("/:|-| /", explode(",", $cmd)[0] );
$ts = mktime($hour, $min, $sec, $month, $day, $year);
$diff = time() - $ts;

if ($diff < 30)
        print ("OK: Last lifesign from modem ".secondsToTime($diff)." ago\n");
        print ("CRITICAL: Last lifesign from modem ".secondsToTime($diff)." ago\n");
Udgivet i Nagios, Uncategorized | Skriv en kommentar

Function to parse contacts from nagios config

$contacts = file_get_contents("/etc/nagios4/objects/auto-contacts.cfg");

function parse_contacts($cfg)
        preg_match_all("/define contact{(.*?)}/mis", file_get_contents($cfg), $matches);
        foreach ($matches[1] as $match)
                preg_match_all("/\s+(.+?)\s+(.+?)\n/mis", $match, $m);
                for ($i=0; $i&lt;count($m[1]); $i++)
                        $contact[$m[1][$i]] = $m[2][$i];
                $contacts[] = $contact;
        return $contacts;

Udgivet i Uncategorized | Skriv en kommentar

Tunneling HP ILO5 through SSH

Creates a tunnel to the ilo port, which in this example is through the jumphost, after this you can connect to ILO by typing in https://locahost and accept the security warning, refreshing and logging in with your credentials.

 ssh -l root -L 443: -L 17990: -L 80:
Udgivet i Linux, SSH | Skriv en kommentar

Unmounting an NFS Share that has gone away

On Linux when an NFS share disappears it is not handled very well, to say it nicely, and in order to get a functioning machine again you need to drop that mount, but a simple umount command states that the device is busy.

So you need to both use the lazy and the force flag:

# umount -l -f /mnt/nfsshare
Udgivet i Linux | Skriv en kommentar

Fail2ban crash course

Just install fail2ban and the default settings will keep you protected just fine, after five failed login attempts the ip address of the offending login will be blocked for 10 minutes using iptables.

# apt-get install fail2ban

If you wish to keep tap on what is happening you can always tail the logfile:

# tail -f /var/log/fail2ban.log
... fail2ban.filter         [2374]: INFO    [sshd] Found - 2022-09-14 22:08:01
... fail2ban.filter         [2374]: INFO    [sshd] Found - 2022-09-14 22:08:05
... fail2ban.filter         [2374]: INFO    [sshd] Found - 2022-09-14 22:08:08
... fail2ban.filter         [2374]: INFO    [sshd] Found - 2022-09-14 22:08:16
... fail2ban.filter         [2374]: INFO    [sshd] Found - 2022-09-14 22:08:20
... fail2ban.actions        [2374]: NOTICE  [sshd] Ban

Status of fail2ban can be shown with the following command:

# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 2
|  |- Total failed:     13
|  `- File list:        /var/log/auth.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     2
   `- Banned IP list:

Unbanning an ip can be done with this command:

# fail2ban-client set sshd unbanip

If you wish to ignore one or more addresses create a file /etc/fail2ban/jail.d/whitelist.conf with the following command:

# echo -e '[DEFAULT]\nignoreip =' > /etc/fail2ban/jail.d/whitelist.conf

And restart fail2ban:

# systemctl restart fail2ban.service
Udgivet i Linux, Security | Skriv en kommentar

Check TLS Certificate with openssl

echo | openssl s_client -servername -connect 2> /dev/null | openssl x509 -noout -dates
notBefore=May  3 06:49:29 2022 GMT
notAfter=Aug  1 06:49:28 2022 GMT
Udgivet i Linux | Skriv en kommentar

Running and monitoring rsync with Python


import subprocess,sys,time,re
import pprint
import string

def execute(cmd):
        process = subprocess.Popen(cmd, shell=True,
        for stdout_line in iter(process.stdout.readline, ""):
                yield stdout_line
        return_code = process.wait()

        if return_code:
                raise subprocess.CalledProcessError(return_code, cmd)

for line2 in execute("rsync --info=progress2 -rl /usr/ ./mc"):
        line = ''.join(c for c in line2 if c.isprintable())
        result ="([0-9\,]+)\s+([0-9]+)%\s+([0-9a-zA-Z\.\/]+)", line)
        if result:
                transfered =
                percentage =
                speed =
                print(f"Rsync process Transfered: {transfered} Percentage: {percentage} Speed: {speed}")
Udgivet i Python | Skriv en kommentar

Printing out a gage in the console with Python


import math, time

def gauge(size, position):
        print("Size: ", size, "Position: ", position)
        block = (size -2) / 100
        print("Block: ", block)

        filled = block * position;

        print("Filled blocks: ", filled)

        print("Filled blocks: ", math.floor(filled))

        for i in range(1,(size -2)):
                if i < filled:
                        print ("#", end="");
                        print (".", end="");

for i in range(1,100):
        print('\033[s', end ='');
        print('\033[u', end = '');
Udgivet i Python | Skriv en kommentar

XCP-NG: Delete default templates

List the templates to get the uuid of the template you wish to delete:

# xe template-list
uuid ( RO)                : 7dd1341e-3261-7a68-f91e-f3625a5e9a97
          name-label ( RW): Debian 11 (Image)
    name-description ( RW): My own

uuid ( RO)                : bfb0c8e5-e1db-4a32-9d85-757b3de0f19f
          name-label ( RW): Debian Bullseye 11
    name-description ( RW): To use this template from the CLI, install your VM using vm-install, then set other-config-install-repository to the path to your network repository, e.g. http://<server>/<path> or nfs:server:/<path>

uuid ( RO)                : 7774689b-4ca1-4dea-8545-dddd6b64c17f
          name-label ( RW): Windows 10 (64-bit)
    name-description ( RW): Clones of this template will automatically provision their storage when first booted and then reconfigure themselves with the optimal settings for Windows 10 (64-bit).


And then remove the parameter “is-default-template” by doing this:

# xe template-param-set is-default-template=false uuid=17a818b5-20a6-4d34-a7ef-320da9ef4c14

And finally delete it by:

xe template-uninstall --force template-uuid=17a818b5-20a6-4d34-a7ef-320da9ef4c14

Udgivet i xcp-ng | Skriv en kommentar