#!/usr/bin/env php
<?php

chdir(dirname($argv[0]));

// Get options before definitions!
$long_opts = array(
	"ipmi-host:",
	"ipmi-port:",
	"ipmi-user:",
	"ipmi-pass:",
	"ipmi-level:",
	"ipmi-interface:",

);

$options = getopt("h:",$long_opts);

include("includes/observium.inc.php");
include("includes/discovery/functions.inc.php");

$cli = TRUE;


$where = '';


if (!$options['h'])  {
    print_message("%n
Usage:
$scriptname -h <device> --ipmi-host ipmihost [--ipmi-port port] --ipmi-user user --ipmi-pass password --ipmi-level <level> --ipmi-interface <type>

-h <device>: hostname | deviceid
--ipmi-host: ipmihostname
--ipmi-port: can be blank, otherwise number
--ipmi-user: the user name for ipmi interface, for instance ADMIN
--ipmi-password: the password for the IPMI user
--ipmi-level: One of user, operator, administrator or callback
--ipmi-interface: the type of the interface, one of lan (IPMI v1.5), lanplus (IPMI v2.0), imb (intel IMB interface) or open (Linux open IPMI interface). 

IPMI options beeing blank are deleted from host. Eg with a call only with hostname as paramter you'll erase the IPMI settings of this host.

%rInvalid arguments!%n", 'color', FALSE);
    exit(-1);
}

if (isset($options['h'])) {
    $params = [];
    $doing = $options['h'];
    $params[] = $options['h'];
    if (is_numeric($options['h'])) {
        $where    = ' AND `device_id` = ?';
     } else {
         $where    = ' AND `hostname` LIKE ?';
     }
}

$moddevice = null;
foreach (dbFetchRows("SELECT * FROM `devices` WHERE `disabled` = 0 $where limit 1", $params) as $device) {
    // Additional check if device SNMPable, because during
    // discovery many devices (long time), the some device can be switched off

    $moddevice = $device; 
}


if ($moddevice === null) {
    print_error("Device ".$options['h']." not found");
    exit(-1);
}

$options['ipmi-level'] = strtoupper($options['ipmi-level']);
$options['ipmi-interface'] = strtolower($options['ipmi-interface']);

if ($options['ipmi-level'] != '' && array_search($options['ipmi-level'], array_keys($config['ipmi']['userlevels'])) !== FALSE) {
    set_entity_attrib('device',$device,'ipmi_userlevel',$options['ipmi-level']);
} else {
  //  del_dev_attrib($device, 'ipmi_userlevel');
    print_error('Invalid user level specified (' . $options['ipmi-level'] . ').');
    exit(-1);
}


if ( $options['ipmi-interface'] != '' && array_search($options['ipmi-interface'], array_keys($config['ipmi']['interface'])) !== FALSE) {
    set_entity_attrib('device',$device,'ipmi_interface',$options['ipmi-interface']);
    print_message("Set interface type to ".$options['ipmi-interface']);
} else {
  //  del_dev_attrib($device, 'ipmi_userlevel');
    print_error('Invalid interface type specified (' . $options['ipmi-interface'] . ').');
    exit(-1);
}


if (isset($options['ipmi-host']) ) {
    set_entity_attrib('device', $device, 'ipmi_hostname', $options['ipmi-host']);
}

if (isset($options['ipmi-user']) ) {
    set_entity_attrib('device', $device, 'ipmi_username', $options['ipmi-user']);
}

if (isset($options['ipmi-pass']) ) {
    set_entity_attrib('device', $device, 'ipmi_password', $options['ipmi-pass']);
}

if (isset($options['ipmi-port']) && is_numeric($options['ipmi-port'])) {
   set_entity_attrib('device', $device, "ipmi_port", $options['ipmi-port']);
} else {
   del_entity_attrib('device', $device, "ipmi_port");
}


// vim:sw=4 ts=4 et
?>
