Added CSV plugin

master
Lukas Macura 2017-01-27 16:05:02 +01:00
parent 75577fa829
commit bc17752cf1
4 changed files with 169 additions and 0 deletions

49
csv/control.zaf Normal file
View File

@ -0,0 +1,49 @@
Plugin: csv
Description::
CSV file reader and parser for Zabbix
It supports reading CSV and getting its data as Zabbix items
Even more it supports discovery based on CSV
::
Version: 0.1
Url: https://raw.githubusercontent.com/limosek/zaf-plugins/master/csv
Web: https://github.com/limosek/zaf-plugins/
# Maintainer
Maintainer: Lukas Macura <lukas@macura.cz>
# Dependencies
Depends-dpkg: dash php-cli
Depends-opkg: busybox php
Install-bin: get_fields.php get_rows.php
Install-files: functions.inc.php
Item discovery.fields:
Parameters::
csv '' ''
columns '1-100' ''
delimiter ',' ''
::
Description::
Returns json field names from csv (header line)
::
Cmd:: get_fields.php
/Item
Item discovery.rows:
Parameters::
csv '' ''
columns '1-100' ''
header 1 ''
delimiter ',' ''
::
Description::
Returns rows from CSV as autodiscovery json.
::
Cmd:: get_rows.php
/Item

51
csv/functions.inc.php Normal file
View File

@ -0,0 +1,51 @@
<?php
set_error_handler('terminate_on_strict');
function terminate_on_strict($errno, $errstr, $errfile, $errline)
{
fprintf(STDERR,"$errstr in $errfile line $errline\n");
exit($errno);
}
function parse_colnum($ranges) {
$groups=preg_split("/,/",$ranges);
foreach ($groups as $group) {
$range=preg_split("/-/",$group);
if (count($range)>1) {
for ($i=$range[0];$i<=$range[1];$i++) {
$columns[]=$i;
}
} else {
$columns[]=$group;
}
}
return($columns);
}
function json_init() {
echo '{ "data":'."\n".' [ '."\n";
}
function json_row() {
echo "{";
}
function json_row_end($last=false) {
if ($last) {
echo " }\n";
} else {
echo " },\n";
}
}
function json_column($name,$value,$last=false) {
echo sprintf('"{#%s}":"%s"',$name,$value);
if (!$last) echo ",";
}
function json_end() {
echo ' ] }'."\n";
}

27
csv/get_fields.php Normal file
View File

@ -0,0 +1,27 @@
#!/usr/bin/php
<?php
require_once(__DIR__."/functions.inc.php");
$csv=getenv("csv");
$range=parse_colnum(getenv("columns"));
$rangef=array_flip($range);
$delim=getenv("delimiter");
$c=fopen($csv,"r");
$header=fgetcsv($c,false,$delim);
fclose($c);
json_init();
$last=end($header);
foreach ($header as $num=>$column) {
if (!array_key_exists($num,$range)) continue;
json_row();
json_column("FIELD","$column",true);
json_row_end($last==$column);
}
json_end();

42
csv/get_rows.php Normal file
View File

@ -0,0 +1,42 @@
#!/usr/bin/php
<?php
require_once(__DIR__."/functions.inc.php");
$csv=getenv("csv");
$range=parse_colnum(getenv("columns"));
$rangef=array_flip($range);
$head=getenv("header");
$delim=getenv("delimiter");
$c=fopen($csv,"r");
if ($head) {
$header=fgetcsv($c,false,$delim);
} else {
$header=Array();
$i=0;
foreach ($range as $c) {
$header[$i]=sprintf("FIELD%d",$i);
$i++;
}
}
json_init();
$line=0;
$last=end($range);
while ($row=fgetcsv($c,false,$delim)) {
$line++;
json_row();
json_column("ROW","$line");
foreach ($range as $num) {
json_column("FIELD$num",$header[$num]);
json_column("VALUE$num",$row[$num],$last==$num);
}
json_row_end(feof($c));
}
json_end();
fclose($c);