Skip to content

Instantly share code, notes, and snippets.

@belotn
Created November 28, 2014 14:29
Show Gist options
  • Save belotn/9ce1d126d3ff12fdfb48 to your computer and use it in GitHub Desktop.
Save belotn/9ce1d126d3ff12fdfb48 to your computer and use it in GitHub Desktop.
Cacti Bulk Add Script
<html><head><title>Ajouter device par lot</title></head><body>
<?php
if(!isset($_POST['add']) && !isset($_FILES['csv']) && !isset($_POST['ds_add'])){
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<label>Fichier CSV</label> <input type="hidden" name="MAX_FILE_SIZE" value="204800" /><input type="file" name="csv"/><br/>
<input type="submit" name="ok" value="Envoyer"/>
</form>
<?php
}elseif(isset($_POST['add'])){
$form = '';
echo "Device Creation, please check end of page for graph option";
foreach($_POST['hostname'] as $key => $val){
$hostname = $val;
$cmd = 'php -q ../cli/add_device.php --description="' . $val . '" --ip="'.$_POST['ip'][$key] . '" --template=' . $_POST['tpl'][$key] .' --community="public" --avail=ping --ping_method=udp --ping_port=23 --ping_retries=3';
$dev = `$cmd`;
echo "Create Device $val :";
echo '<pre>' . $dev . '</pre>';
preg_match('/ew device-id:\s\((\d+)\)/', $dev, $match);
$devid = $match[1];
$cmd = 'php -q ../cli/add_tree.php --type=node --node-type=host --tree-id=' . $_POST['tree'][$key] . ' --host-id=' . $devid;
$add = `$cmd`;
echo 'Append to tree : ' . $_POST['tree'][$key];
echo '<pre>' . $add . '</pre>';
$cmd = 'php -q ../cli/add_graphs.php --list-graph-templates --host-template-id=' . $_POST['tpl'][$key];
$graph = `$cmd`;
echo '<pre>' . $graph . '</pre>';
//check if graph need input fields
$need_input = false;
$aInField=array();
foreach(explode("\n", $graph) as $line ){
$tmp = explode("\t",$line);
if(is_numeric($tmp[0])){
$cmd = 'php -q ../cli/add_graphs.php --list-input-fields --graph-template-id='. $tmp[0];
$input_fields = `$cmd`;
$j=0;
foreach(explode("\n",$input_fields) as $field){
if($j==0){
$j++;
}elseif(strlen(trim($field)) > 0 ){
$val = explode("\t", $field);
$val = explode(":", $val[0]);
$aInField[]=array('graphid' => $tmp[0],'graphname' => $tmp[1],'host' => $val, 'field' => $val[1]);
$need_input=true;
}
}
}
}
if(!$need_input){
foreach(explode("\n", $graph) as $line){
$tmp = explode("\t",$line);
if(is_numeric($tmp[0])){
$cmd = 'php -q ../cli/add_graphs.php --host-id=' . $devid . ' --graph-type=cg --graph-template-id='.$tmp[0];
$create = `$cmd`;
echo "Associated GRaph : " . $tmp[1] . '<br/>';
echo '<pre>' . $create . '</pre>';
}
}
}else{
//generate Forms
if($form == ''){
$form = '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
}
$form .= '<input type="hidden" name="device[]" value="' . $devid . '"/>' . $hostname;
foreach($aInField as $entry){
$form .= '<input type="hidden" name ="graph[' . $devid . '][]" value="' . $entry['graphid'] . '"/>
<input type="hidden" name="field[' . $devid . '][]" value="' . $entry['field'] . '"/><label>' . $entry['graphname'] . ' - ' . $entry['field'] . '
<input type="text" name="value[' . $devid . '][]" /><br/>';
}
}
}
if($form) echo $form . '<input type="submit" value="Go!" name="ds_add" /></form>';
}elseif(isset($_POST['ds_add'])){
foreach($_POST['device'] as $device){
$cmd = 'php -q ../cli/add_graphs.php --host-id=' . $device . ' --graph-type=cg ';
$acmd = array();
foreach( $_POST['graph'][$device] as $key => $graph){
$cmd.=' --graph-template-id=' . $graph . ' --input-fields="' . $_POST['field'][$device][$key] . '=' . $_POST['value'][$device][$key] . '"';
$create = `$cmd`;
print '<pre>$create</pre><br/>';
}
}
}else{
if (($handle = fopen($_FILES['csv']['tmp_name'], "r")) !== FALSE) {
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
$fields = fgetcsv($handle,1000,";");
$str_tpl = `php -q ../cli/add_device.php --list-host-templates`;
$options = ""; $i = false;
foreach( explode("\n", $str_tpl) as $line){
$line = trim($line);
if(strlen($line)> 0 && $i ) {
$tmp = explode("\t", $line);
$options .= '<option value="' . $tmp[0] . '">' . $tmp[1]. '</option>';
}
if(!$i){
$i=true;
}
}
$str_tree = `php -q ../cli/add_tree.php --list-trees`;
$i=0;$trees = "";
foreach( explode("\n", $str_tree) as $line){
$line = trim($line);
if(strlen($line) > 0 && $i > 1){
$tmp = explode("\t", $line);
$trees.= '<option value="' . $tmp[0] . '">' . $tmp[2] . '</option>';
}
$i++;
}
while(($data = fgetcsv($handle,1000,";"))!= FALSE){
echo '<label for="hostname">new device</label>';
echo '<input id="hostname" type="text" disable="disabled" name="hostname[]" value="' . $data[0]. '"/>';
echo '<label for="ip">IP</label>';
echo '<input id="ip" type="text" name="ip[]" disable="disabled" value="' . $data[2] . '"/>';
echo '<label for="tpl">Template</label>';
echo '<select name="tpl[]">' . $options . '</select>';
echo '<label>Tree</label>';
echo '<select name="tree[]">' . $trees . '</select><br/>';
}
echo '<input type="submit" name="add" value="Enregistrer" /></form>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment