From 0a36456b56fcfae7e83ec2ff3869903749ab4dc4 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Mon, 30 Jan 2017 14:06:12 +0100 Subject: [PATCH] Added doc for csv --- Makefile | 12 ---- booked/README.md | 5 +- csv/README.md | 155 ++++++++++++++++++++++++++++++++++++++++++ csv/functions.inc.php | 2 +- csv/get_rows.php | 2 +- csv/register | 1 + csv/send | 16 +++-- iwx/plugin.mk | 34 --------- lib.mk | 43 ------------ 9 files changed, 171 insertions(+), 99 deletions(-) delete mode 100644 Makefile create mode 100644 csv/README.md delete mode 100644 iwx/plugin.mk delete mode 100644 lib.mk diff --git a/Makefile b/Makefile deleted file mode 100644 index eeb1b68..0000000 --- a/Makefile +++ /dev/null @@ -1,12 +0,0 @@ - -include lib.mk - -ifeq ($(PLUGINS),) - PLUGINS=$(shell ls -I '*mk' -I Makefile -I README.md) -endif - -all: $(PLUGINS) - -$(eval $(call Plugins/Rules)) - - diff --git a/booked/README.md b/booked/README.md index 81cbc3e..eb9e268 100644 --- a/booked/README.md +++ b/booked/README.md @@ -4,10 +4,11 @@ Booked is opensource reservation system using PHP. More info can be found at htt This Zaf plugin supports fetching informations from this system and find if there is some reservation in given time range. This is usefull for scripting and alerting. -If you need more info about installing zaf: https://github.com/limosek/zaf - ## How to use +**Note:** If you need more info about installing zaf: https://github.com/limosek/zaf +**Note**: You can find more examples on [my site](https://macura.cz/search/node?keys=zaf) + First, create account in Booked with required access to system and enable API. Next, simply install ``` zaf install booked diff --git a/csv/README.md b/csv/README.md new file mode 100644 index 0000000..a85740f --- /dev/null +++ b/csv/README.md @@ -0,0 +1,155 @@ +# CSV support for Zaf + +This plugin tries to automate some basic scenarios using CSV as input files. If you have some data in CSV which ypu want to discover and analyze in Zabbix, this is right place to do so. +``` +Plugin 'csv' version 0.1: + 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 + +Maintainer: Lukas Macura +Url: https://raw.githubusercontent.com/limosek/zaf-plugins/master/csv + +Defined items: csv.discovery.fields[] csv.discovery.rows[] + +``` + +## How to use + +**Note:** If you need more info about installing zaf: https://github.com/limosek/zaf +**Note**: You can find more examples on [my site](https://macura.cz/search/node?keys=zaf) + +Simply instal zaf and after, install csv plugin +``` +zaf install csv +``` + +### Items ### + +``` +Item csv.discovery.rows + +Parameters: + csv '' '' + columns '1-100' '' + rows '1-1000' '' + header 1 '' + delimiter ',' '' + +``` +With given parameters, plugin will return JSON object suitable for autodiscovery. For example, to discover contents of /etc/passwd: +* You can enter range of columns in format 1-3, it means column numbers from 1 to 3. +* Even more, there can be only one number. +* And you can enter more ranged divided by '_' +* Same for rows +* This example: columns 1,2,3 and rows 5-6, file has no header and fields are separated by ':' +``` +zaf run csv.discovery.rows[/etc/passwd,1-2_3,5-6,0,:] +{ + "data" : [ + { + "{#COLUMN0}" : "FIELD0", + "{#VALUE0}" : "root", + "{#VALUE1}" : "x", + "{#ROW}" : "1", + "{#COLUMN2}" : "FIELD2", + "{#COLUMN1}" : "FIELD1", + "{#VALUE2}" : "0" + }, + { + "{#VALUE1}" : "x", + "{#COLUMN2}" : "FIELD2", + "{#ROW}" : "2", + "{#COLUMN1}" : "FIELD1", + "{#VALUE2}" : "1", + "{#VALUE0}" : "daemon", + "{#COLUMN0}" : "FIELD0" + } + ] +} +``` + +``` +Item csv.discovery.fields + +Parameters: + csv '' '' + columns '1-100' '' + delimiter ',' '' +``` +To discovery columns from CSV (first line of it). +``` +{ + "data" : [ + { + "{#NAME}" : "root", + "{#COLUMN}" : "0" + }, + { + "{#NAME}" : "x", + "{#COLUMN}" : "1" + }, + { + "{#COLUMN}" : "2", + "{#NAME}" : "0" + } + ] +} +``` + +### Subcommands ### +Subcommands can be entered by 'zaf csv'. + +To send entire CSV file (or its part) by zabbix sender: + +``` +zaf csv send +Missing arguments! +send file.csv delim mode item1=field1 [item2=field2] ... +mode is stdout or send, +itemx is key for item to send, +fieldx is data to send send, +In itemx and fieldx are replaced this macros: +{COLUMN:x} is replaced by value of column x +{column:x} is replaced by lowercased value of column x +x can be column index (x starts with zero) or header name. +CSV must include header line. + +``` + +To register hosts to Zabbix server based on CSV: +``` +zaf csv register +Missing arguments! +register file.csv delim host metadata +host and metadata are strings where: +{COLUMN:x} is replaced by value of column x +{column:x} is replaced by lowercased value of column x +x can be column index (x starts with zero) or header name. +CSV must include header line. +``` + +### Example ### + +There is CSV file with this columns: +Host|CPU|Vulns|Services +coffee-Maker|arm|lotof|hot coffee +usb-Gun|mips|none|calm thunder + +If you want to add all of hosts to Zabbix, simple use autodiscovery action based on metadata content. For example, all autodiscovered hosts with metadata ZAF will create host. +Suppose you want to prefix each hostname by 'test_' and you want to lowercase it. +``` +zaf csv register 'hosts.csv' ',' 'test_{column:Host}' ZAF +``` +This command will autoregister two hosts test_cofee-maker and test_usb-gun. + +Next you want to fill data. You created template with right items as zabbix trapper items. +``` +zaf csv send 'hosts.csv' ',' 'stdout' {COLUMN:Host} 'vulns={column:Vulns}' 'services={column:Services}' +``` +This will show what to send by zabbix_sender. To actualy send it, you can use +``` +zaf csv send 'hosts.csv' ',' 'send' test_{COLUMN:Host} 'vulns={column:Vulns}' 'services={column:Services}' +``` + + diff --git a/csv/functions.inc.php b/csv/functions.inc.php index 2c68b10..a8c4a7a 100644 --- a/csv/functions.inc.php +++ b/csv/functions.inc.php @@ -10,7 +10,7 @@ function terminate_on_strict($errno, $errstr, $errfile, $errline) } function parse_colnum($ranges) { - $groups=preg_split("/,/",$ranges); + $groups=preg_split("/[,_]/",$ranges); foreach ($groups as $group) { $range=preg_split("/-/",$group); if (count($range)>1) { diff --git a/csv/get_rows.php b/csv/get_rows.php index ae045a6..f38a54e 100644 --- a/csv/get_rows.php +++ b/csv/get_rows.php @@ -18,7 +18,7 @@ if ($head) { } else { $header=Array(); $i=0; - foreach ($range as $c) { + foreach ($range as $r) { $header[$i]=sprintf("FIELD%d",$i); $i++; } diff --git a/csv/register b/csv/register index 5ce15ca..9f58a3f 100644 --- a/csv/register +++ b/csv/register @@ -27,6 +27,7 @@ $c=fopen($csv,"r"); $header=fgetcsv($c,false,$delim); while ($row=fgetcsv($c,false,$delim)) { + if (count($row)==1 && trim($row[0])=="") continue; $p=get_replacements($header,$row); $hostr=preg_replace($p["patterns"],$p["replacements"],$host); $metadatar=preg_replace($p["patterns"],$p["replacements"],$metadata); diff --git a/csv/send b/csv/send index 73806f5..92d8f92 100644 --- a/csv/send +++ b/csv/send @@ -3,13 +3,14 @@ require_once(__DIR__."/functions.inc.php"); -if ($argc<5) { +if ($argc<6) { fprintf(STDERR,"Missing arguments!\n"); - fprintf(STDERR,"send file.csv delim mode item1=field1 [item2=field2] ...\n"); + fprintf(STDERR,"send file.csv delim mode hostfield [item1=field1] [item2=field2] ...\n"); fprintf(STDERR,"mode is stdout or send,\n"); + fprintf(STDERR,"hostfield is host for item to 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,"In hostfield, 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"); @@ -23,8 +24,9 @@ if ($csv=="-") { } $delim=$argv[2]; $mode=$argv[3]; +$host=$argv[4]; $items=Array(); -for ($i=4;$i<$argc;$i++) { +for ($i=5;$i<$argc;$i++) { List($key,$field)=preg_split("/=/",$argv[$i]); $items[]=Array( "key" => $key, @@ -38,12 +40,14 @@ $header=fgetcsv($c,false,$delim); $data=""; while ($row=fgetcsv($c,false,$delim)) { + if (count($row)==1 && trim($row[0])=="") continue; $p=get_replacements($header,$row); + $hostr=preg_replace($p["patterns"],$p["replacements"],$host); foreach ($items as $item) { $data.=sprintf("%s %s %s\n", - getenv("ZAF_HOSTNAME"), + $hostr, preg_replace($p["patterns"],$p["replacements"],$item["key"]), - preg_replace($p["patterns"],$p["replacements"],$item["field"]) + addcslashes(preg_replace($p["patterns"],$p["replacements"],$item["field"]),"\0..\40\"") ); } } diff --git a/iwx/plugin.mk b/iwx/plugin.mk deleted file mode 100644 index 914e6ea..0000000 --- a/iwx/plugin.mk +++ /dev/null @@ -1,34 +0,0 @@ - -NAME=iwx -VERSION=0.5 -URL=https://raw.githubusercontent.com/limosek/zaf-plugins/master/iwx -WEB=https://github.com/limosek/zaf-plugins -DEPENDS_OPKG=busybox jshn -DEPENDS_DPKG=dash -DEPENDS_RPM=dash -SUDO=iw -INSTALL_BIN=functions.sh - -ITEMS=if_discovery phy_discovery - -define iwx/Description - Plugin for wifi mac80211 informations and discovery -endef - -define iwx/Item/if_discovery/Description - Discovery of wireless interfaces -endef - -define iwx/Item/if_discovery/Cmd - wifi_if_discovery -endef - -define iwx/Item/phy_discovery/Description - Discovery of wireless physical interfaces -endef - -define iwx/Item/phy_discovery/Cmd - wifi_phy_discovery -endef - - diff --git a/lib.mk b/lib.mk deleted file mode 100644 index d506833..0000000 --- a/lib.mk +++ /dev/null @@ -1,43 +0,0 @@ - -define shvar -V_$(subst .,_,$(subst -,_,$(subst /,_,$(1)))) -endef - -define shexport -export $(call shvar,$(1))="$$(call $(1))" -endef - -define newline - - -endef - -define Plugin/Rule - ifeq ($(wildcard $(1)/plugin.mk),) - $(1): - $(MAKE) PLUGINS=$(1) $(1)/control.zaf - $(1)/control.zaf: - @echo -n - else -include $(1)/plugin.mk - $(1): $(1)/control.zaf - $(MAKE) PLUGINS=$(1) $(1)/control.zaf - $(1)/plugin.mk: - $(1)/control.zaf: $(1)/plugin.mk - printf %b '$(subst $(call newline),\n,$(call Plugin/Control,$(1)))' - - endif -endef - -define Plugins/Rules - $(foreach p,$(PLUGINS),$(eval $(call Plugin/Rule,$(p)))) -endef - -define Plugin/Control -Plugin: $(1) -Version: $$(VERSION) -Description:: - $$(call $(1)/Description) -:: -endef -