Ad Widget

Collapse

How to use PHP scripts in Zabbix external items

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rd67
    Junior Member
    • Jul 2024
    • 8

    #1

    How to use PHP scripts in Zabbix external items

    I couldn't find how to use php scripts in zabbix items, so i want to share what i did on my zabbix server.

    LINUX SHELL

    installed php (syntax using debian)
    Code:
    apt install php
    installed php ssh; this is needed only if you want to use php to run remote commands via ssh
    (i did it because the embedded ssh zabbix item won't work with multi line commands on cisco ios devices)
    Code:
    apt install php-phpseclib3 php-phpseclib-net-ssh2
    created a link to php in the zabbix script dir:
    Code:
    cd /usr/lib/zabbix/externalscripts
    ln -s /usr/bin/php
    now you can write php scripts, store them in the above path and use them in zabbix external items

    For example I did this to run ssh scripts on cisco devices:
    - saved a php script file named "cisco_run_command.php" in /usr/lib/zabbix/externalscripts
    Code:
    <?php
    $host = $argv[1];
    $user = $argv[2];
    $pswd = $argv[3];
    $prompt = $argv[4];
    $command = $argv[5];
    include("/usr/share/php/Net/SSH2.php");
    $port = 22;
    $timeout = 20;
    $ssh = new Net_SSH2($host, $port ,$timeout );
    if (!$ssh->login($user, $pswd )) {
    exit('Login Failed');
    }
    $ssh->read('#');
    $ssh->write("term len 0 \n"); // note the "\n"
    $ssh->read('#');
    $ssh->write("$command \n"); // note the "\n"
    $output = $ssh->read($prompt);
    echo $output;

    ZABBIX WEB GUI

    - created an item (either directly in a host or in a template if you want to use it many times):
    • Type: External Item
    • Key: php["/usr/lib/zabbix/externalscripts/cisco_run_command.php","{HOST.IP}","{$CISCO_USER}" ,"{$CISCO_PWD}",{$CISCO_PROMPT},{$CISCO_CMD2RUN }]
    • Type of informantion: Text <-- or whatever you except to retrieve from the command executed on the cisco device.

    In the host where the above item is used (i guess some\all macros could be set at the Administration\Macro level, if shared among many hosts)
    Macros:
    • {$CISCO_USER} = "yousername"
    • {$CISCO_PWD} = "yourpasswd"
    • {$CISCO_PROMPT} = "hostname#" <-- this is the prompt shown on the device after a sucessful ssh login
    • {$CISCO_CMD2RUN} = "show run" <-- or any other cisco command

    DISCLAIMER:
    I'm very new to zabbix and I'm not a programmer so it's likely that what i did could be improved or maybe there's a better\smarter way to do it (if so please let me know).
    Also, no idea if there are any security implications.

    ​​
Working...