mirror of
https://github.com/limosek/zaf-plugins.git
synced 2025-01-21 07:25:56 +01:00
Added subcommands register and send
This commit is contained in:
parent
bc17752cf1
commit
6ee868cf6d
@ -21,6 +21,7 @@ Depends-opkg: busybox php
|
||||
|
||||
Install-bin: get_fields.php get_rows.php
|
||||
Install-files: functions.inc.php
|
||||
Install-cmd: register send
|
||||
|
||||
Item discovery.fields:
|
||||
Parameters::
|
||||
@ -38,6 +39,7 @@ Item discovery.rows:
|
||||
Parameters::
|
||||
csv '' ''
|
||||
columns '1-100' ''
|
||||
rows '1-1000' ''
|
||||
header 1 ''
|
||||
delimiter ',' ''
|
||||
::
|
||||
|
@ -49,3 +49,20 @@ function json_end() {
|
||||
echo ' ] }'."\n";
|
||||
}
|
||||
|
||||
function get_replacements($header,$row) {
|
||||
$patterns=Array();
|
||||
$replacements=Array();
|
||||
foreach ($header as $n=>$h) {
|
||||
$patterns[]="/{COLUMN:$n}/";
|
||||
$replacements[]=$row[$n];
|
||||
$patterns[]="/{COLUMN:$h}/";
|
||||
$replacements[]=$row[$n];
|
||||
$patterns[]="/{column:$n}/";
|
||||
$replacements[]=strtolower($row[$n]);
|
||||
$patterns[]="/{column:$h}/";
|
||||
$replacements[]=strtolower($row[$n]);
|
||||
}
|
||||
return(Array("patterns" => $patterns, "replacements" => $replacements));
|
||||
}
|
||||
|
||||
|
||||
|
@ -14,12 +14,15 @@ fclose($c);
|
||||
|
||||
json_init();
|
||||
|
||||
$last=end($header);
|
||||
$last1=end($header);
|
||||
$last2=end($range);
|
||||
|
||||
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_column("COLUMN",$num);
|
||||
json_column("NAME",addslashes($column),true);
|
||||
json_row_end($last1==$column||$last2==($num+1));
|
||||
}
|
||||
|
||||
json_end();
|
||||
|
@ -5,6 +5,8 @@ require_once(__DIR__."/functions.inc.php");
|
||||
|
||||
$csv=getenv("csv");
|
||||
$range=parse_colnum(getenv("columns"));
|
||||
$rows=parse_colnum(getenv("rows"));
|
||||
$rowsf=array_flip($rows);
|
||||
$rangef=array_flip($range);
|
||||
$head=getenv("header");
|
||||
$delim=getenv("delimiter");
|
||||
@ -25,16 +27,19 @@ if ($head) {
|
||||
json_init();
|
||||
$line=0;
|
||||
$last=end($range);
|
||||
$lastrow=max($rows);
|
||||
|
||||
while ($row=fgetcsv($c,false,$delim)) {
|
||||
$line++;
|
||||
if (!array_key_exists($line,$rowsf)) continue;
|
||||
json_row();
|
||||
json_column("ROW","$line");
|
||||
foreach ($range as $num) {
|
||||
json_column("FIELD$num",$header[$num]);
|
||||
json_column("VALUE$num",$row[$num],$last==$num);
|
||||
$col=$num-1;
|
||||
json_column("COLUMN$col",$header[$col]);
|
||||
json_column("VALUE$col",addslashes($row[$col]),$last==$num);
|
||||
}
|
||||
json_row_end(feof($c));
|
||||
json_row_end(feof($c)||$line==$lastrow);
|
||||
}
|
||||
json_end();
|
||||
|
||||
|
38
csv/register
Normal file
38
csv/register
Normal file
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
require_once(__DIR__."/functions.inc.php");
|
||||
|
||||
if ($argc<4) {
|
||||
fprintf(STDERR,"Missing arguments!\n");
|
||||
fprintf(STDERR,"register file.csv delim host metadata\n");
|
||||
fprintf(STDERR,"host and metadata are strings where:\n");
|
||||
fprintf(STDERR,"{COLUMN:x} is replaced by value of column x\n");
|
||||
fprintf(STDERR,"{column:x} is replaced by lowercased value of column x\n");
|
||||
fprintf(STDERR,"x can be column index (x starts with zero) or header name.\n");
|
||||
fprintf(STDERR,"CSV must include header line.\n\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$csv=$argv[1];
|
||||
if ($csv=="-") {
|
||||
$csv="php://stdin";
|
||||
}
|
||||
$delim=$argv[2];
|
||||
$host=$argv[3];
|
||||
$metadata=$argv[4];
|
||||
|
||||
$c=fopen($csv,"r");
|
||||
|
||||
$header=fgetcsv($c,false,$delim);
|
||||
|
||||
while ($row=fgetcsv($c,false,$delim)) {
|
||||
$p=get_replacements($header,$row);
|
||||
$hostr=preg_replace($p["patterns"],$p["replacements"],$host);
|
||||
$metadatar=preg_replace($p["patterns"],$p["replacements"],$metadata);
|
||||
fprintf(STDERR,getenv("ZAF_BIN")." register $hostr $metadatar\n");
|
||||
system(getenv("ZAF_BIN")." register $hostr $metadatar\n");
|
||||
}
|
||||
|
||||
fclose($c);
|
||||
|
68
csv/send
Normal file
68
csv/send
Normal file
@ -0,0 +1,68 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
require_once(__DIR__."/functions.inc.php");
|
||||
|
||||
if ($argc<5) {
|
||||
fprintf(STDERR,"Missing arguments!\n");
|
||||
fprintf(STDERR,"send file.csv delim mode item1=field1 [item2=field2] ...\n");
|
||||
fprintf(STDERR,"mode is stdout or send,\n");
|
||||
fprintf(STDERR,"itemx is key for item to send,\n");
|
||||
fprintf(STDERR,"fieldx is data to send send,\n");
|
||||
fprintf(STDERR,"In itemx and fieldx are replaced this macros:\n");
|
||||
fprintf(STDERR,"{COLUMN:x} is replaced by value of column x\n");
|
||||
fprintf(STDERR,"{column:x} is replaced by lowercased value of column x\n");
|
||||
fprintf(STDERR,"x can be column index (x starts with zero) or header name.\n");
|
||||
fprintf(STDERR,"CSV must include header line.\n\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$csv=$argv[1];
|
||||
if ($csv=="-") {
|
||||
$csv="php://stdin";
|
||||
}
|
||||
$delim=$argv[2];
|
||||
$mode=$argv[3];
|
||||
$items=Array();
|
||||
for ($i=4;$i<$argc;$i++) {
|
||||
List($key,$field)=preg_split("/=/",$argv[$i]);
|
||||
$items[]=Array(
|
||||
"key" => $key,
|
||||
"field" => $field
|
||||
);
|
||||
}
|
||||
|
||||
$c=fopen($csv,"r");
|
||||
|
||||
$header=fgetcsv($c,false,$delim);
|
||||
|
||||
$data="";
|
||||
while ($row=fgetcsv($c,false,$delim)) {
|
||||
$p=get_replacements($header,$row);
|
||||
foreach ($items as $item) {
|
||||
$data.=sprintf("%s %s %s\n",
|
||||
getenv("ZAF_HOSTNAME"),
|
||||
preg_replace($p["patterns"],$p["replacements"],$item["key"]),
|
||||
preg_replace($p["patterns"],$p["replacements"],$item["field"])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fclose($c);
|
||||
|
||||
if ($mode=="send") {
|
||||
$h = popen(getenv("ZAF_BIN")." send", "w");
|
||||
fputs($h,$data);
|
||||
fclose($h);
|
||||
exit;
|
||||
}
|
||||
if ($mode=="stdout") {
|
||||
echo $data;
|
||||
exit;
|
||||
}
|
||||
|
||||
fprintf(STDERR,"Bad mode $mode\n!");
|
||||
exit(1);
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user