Mail_package_platform_status.php - pfSense update notifier via mail



  • Hi.

    I release a php cli script to notify pfSense updates via e-mail.

    For those who are interested, I have it in:

    https://www.javcasta.com/bounties/#notifier_package_plarform

    Happy Winter Solstice ;)

    The code.

    
    /*
    #####################################################
    #   "/usr/games/mail_package_platform_status.php"   #
    # ------------------------------------------  #######
    #Notificador de updates de packages & platform#
    #               vía email                     #
    #Notifier package & platform updates via email#
    # --------------------------------------------#
    # Código adaptado y modificado de:            #
    #  Modified and adapted code from:            # 
    # "/usr/local/www/pkg_mgr_installed.php"      #
    # By JavCasta - PIyMenta - Javier Castañon    #
    #     2016 --- https://javcasta.com/          #
    #################################################
    # Tested in - Testeado en - pfSense 2.3.2 amd64 #     
    #################################################
    # Happy winter solstice #
    #########################
    Condiciones necesarias - Neccesary conditions:
    -----------------------
    - Tener configurado y operativo - Having configured and operational:
      System > Advanced > Notifications > E-Mail
    - Crear un cron (aconsejable en el paquete cron) que ejecute cada día o cada 12 o 24 hrs:
      Create a (recommended in the package cron) cron running every day or every 12 or 24 hrs:
    
      /usr/local/bin/php /usr/games/mail_package_platform_status.php 2>&1
    
    - Alojar fichero mail_package__platform_status.php en:
      Host file mail_package_platform_status.php in:
      /usr/games/
    */
    
    //<title>PiyMenta - @javcasta - 2016</title>
    // If arrives here, is a valid user.
    require_once("pkg-utils.inc");
    echo "Notificador status package & platform via e-mail - Notifier package & platform updates via email \n";
    
    //el nombre del pfSense
    $mipfsense = gethostname();
    echo "$mipfsense \n";
    
    if (is_subsystem_dirty('packagelock')) {
    	echo "Hay paquetes instalandose o actualizandose - Settling packages or packages are being updated \n";
    	exit;
    }
    
    $laplataforma = "";
    $system_version = get_system_pkg_version();
    if ($system_version === false) {
        $laplataforma = "Unable to check for updates";
    		//exit;
    	}
    if (!is_array($system_version) ||
    	    !isset($system_version['version']) ||
    	    !isset($system_version['installed_version'])) {
        $laplataforma = "Error in version information";
    	}
    $version_compare = pkg_version_compare(
    	    $system_version['installed_version'], $system_version['version']);
    
    	switch ($version_compare) {
    	case '<':
        $laplataforma = "The system not is on the latest version";
    		break;
    	case '=':
    		//print(gettext("The system is on the latest version."));
       $laplataforma = "The system is on the latest version.";
    		break;
    	case '>':
    		//print(gettext("The system is on a later version than
    the official release."));
       $laplataforma = "The system is on a later version than the official release.";
    		break;
    	default:
    		//print(gettext( "_Error comparing installed version
    with latest available_"));
       $laplataforma = "Error comparing installed version with latest available.";
    		break;
    	}
      $resultadoplataforma = $laplataforma." Version: ".$system_version['version'];
      echo "\n".$resultadoplataforma."\n";
    if (strpos($laplataforma, "The system not is on the latest version") !== false) {
        //echo 'true';
        $comando = "echo " . '"' . $resultadoplataforma . '"' . ' | ' . "/usr/local/bin/mail.php -s=". '"' . "$mipfsense : Upgrade platform available - Upgrade plataforma disponible" . '"';
        shell_exec("$comando");
        shell_exec("logger -f /var/log/system.log 'Notificado update plataforma'");
        echo "\n Upgrade platform available. Sending mail - Hay upgrade de plataforma disponible. Enviando mail \n";
    } else { 
              echo "\n No Upgrade platform available, no mail - No hay upgrade de plataforma disponible. No se envia mail. \n";
              shell_exec("logger -f /var/log/system.log 'NO hay update plataforma'");
            }
    
    $estado1 = "\n";
    //obtenemos estado de paquetes
    $estado = get_pkg_status();
    $estado1 .= "\n". $estado . "\n";
    $estado1 = str_replace("
    ", ' || ', $estado1);
    echo "$estado1";
    //si existe Upgrade available enviamos email
    if (strpos($estado, "Upgrade available") !== false) {
        //echo 'true';
        $estado = str_replace("
    ", ' || ', $estado);
        $comando = "echo " . '"' . $estado . '"' . ' | ' . "/usr/local/bin/mail.php -s=". '"' . "$mipfsense : Updates availables - Updates disponibles" . '"';
        shell_exec("$comando");
        shell_exec("logger -f /var/log/system.log 'Notificado update paquete'");
        echo "\n Updates packages availables. Sending mail - Hay updates de paquetes. Enviando mail \n";
    } else { echo "\n No Updates packages, no mail - No hay updates de paquetes. No se envia mail. \n";
            shell_exec("logger -f /var/log/system.log 'No hay update paquete'");
            }
    /*
    Para msgs con Content-type: text/html usar otro script que mail.php
    mail.php es para mail básico a ejecutar via shell 
    */
    echo "\n https://www.paypal.me/javcasta/5 - Feel free to Paypal me \n";
    //################
    
    function get_pkg_status() {
    	$installed_packages = array();
    	$package_list = get_pkg_info();
    
    	if (!$package_list) {
    		//print("error");
        return "error";
    		exit;
    	}
    
    	foreach ($package_list as $pkg) {
    		if (!isset($pkg['installed']) && !isset($pkg['broken'])) {
    			continue;
    		}
    		$installed_packages[] = $pkg;
    	}
    
    	$pkgtbl = "";
    
    	if (empty($installed_packages)) {
    		//print ("nopkg");
        return "nopkg";
    		exit;
    	}
    
      $pkgtbl .= "
    ";
    
    	foreach ($installed_packages as $pkg) {
    		if (!$pkg['name']) {
    			continue;
    		}
    
    		#check package version
    		//$txtcolor = "";
    		$upgradeavail = false;
    		$missing = false;
    		$vergetstr = "";
    
    		if (isset($pkg['broken'])) {
    			// package is configured, but does not exist in the system
    			$missing = true;
    			$status = $pkg['name']. ' ' . gettext('Package is configured, but not installed!');
    		} else if (isset($pkg['installed_version']) && isset($pkg['version'])) {
    			$version_compare = pkg_version_compare($pkg['installed_version'], $pkg['version']);
    
    			if ($version_compare == '>') {
    				// we're running a newer version of the package
    				$status = $pkg['name'] . ' ' . gettext('Newer than available (%s)') . ' ' . $pkg['version'];
    
    			} else if ($version_compare == '<') {
    				// we're running an older version of the package
    				$status = $pkg['name'] . ' ' . gettext("Upgrade available to ") . ' ' . $pkg['version'] . " - ";
    				$upgradeavail = true;
    				$vergetstr = $pkg['installed_version'] . ' to ' . $pkg['version'];
    			} else if ($version_compare == '=') {
    				// we're running the current version
    				$status = $pkg['name']. ' ' . gettext('Up-to-date');
    			} else {
    				$status = gettext('Error comparing version');
    			}
    		} else {
    			// unknown available package version
    			$status = gettext('Unknown');
    		}
    
    		if ($upgradeavail) {
    			$pkgtbl .= $status . $pkg['name'] . $vergetstr . "
    ";
    		} elseif ($missing) {
    			$pkgtbl .= $status . "
    ";
    		} else {
    			$pkgtbl .= $status . "
    ";
    		}
    
    	}
    
    	return $pkgtbl;
    }
    ?>
    
    

    Regards.



  • Hi.

    An output to script executation.

    [2.3.2-RELEASE][root@pfSense1.localdomain]/: /usr/local/bin/php /usr/games/mail_package_platform_status.php
    Notificador status package & platform via e-mail - Notifier package & platform updates via email
    pfSense1.localdomain

    The system is on the latest version. Version: 2.3.2_1

    No Upgrade platform available, no mail - No hay upgrade de plataforma disponible. No se envia mail.

    || pfSense-pkg-Cron Up-to-date || pfSense-pkg-Lightsquid Up-to-date || pfSense-pkg-mailreport Up-to-date || pfSense-pkg-nmap Up-to-date || pfSense-pkg-nrpe Up-to-date || pfSense-pkg-openvpn-client-export Upgrade available to  1.3.15 - pfSense-pkg-openvpn-client-export1.3.13 to 1.3.15 || pfSense-pkg-pfBlockerNG Up-to-date || pfSense-pkg-Service_Watchdog Up-to-date || pfSense-pkg-snort Up-to-date || pfSense-pkg-squid Up-to-date || pfSense-pkg-squidGuard Up-to-date ||

    Updates packages availables. Sending mail - Hay updates de paquetes. Enviando mail

    Regards.



  • Work fine to me. Happy  winter solstice ;D



  • Hi.

    I'm so glad :)

    Regards.



  • Hello Javcasta
    This is great thank you.
    I have a question, I need to replace the following IPfire script line with something works with pfsense:
    #/usr/local/bin/sendEmail -f XXXXXXX@gmail.com -t XXXXXXX@gmail.com -u "P-Logs" -m " Attached log file " -s smtp.gmail.com:587 -xu XXXXXXX -xp XXXXXXX  -o tls=yes -a /home/scripts/poxlog.txt;

    In other world how do I get mail.php to send an email with an attachment file..

    Thank you
    I