diff --git a/osfv_cli/monitoring/zabbix/External_Check.md b/osfv_cli/monitoring/zabbix/External_Check.md new file mode 100644 index 0000000..65d6ece --- /dev/null +++ b/osfv_cli/monitoring/zabbix/External_Check.md @@ -0,0 +1,84 @@ +# External Check Zabbix + +This is a brief guide on setting up external checks in Zabbix. +You can find example scripts in this folder. + +## Preparation + +1. Make sure your script is in + +```C +/usr/lib/zabbix/externalscripts +``` + +> The default location can be changed in `/etc/zabbix/zabbix_server.conf` +> by editing `ExternalScripts=/usr/lib/zabbix/externalscripts` + +**Ensure that you have only one tab open when creating anything in Zabbix (items/triggers/hosts).** + +> It's likely that the last used tab is overwriting session data. + +## Create the item in Zabbix + +> Item is a tool that gathers data from device, system or external script + +1. Navigate to **Configuration** > **Hosts**. +1. Go to the **Items** next to the host you want to monitor. +1. Click **Create item** in top right. +- Change Type to **External check**. +- As key use your script name together with desired **Macro**, for example `script.sh[{HOST.IP}]`. + +> [External Checks](https://www.zabbix.com/documentation/current/en/manual/config/items/itemtypes/external) +> [List of macros](https://www.zabbix.com/documentation/current/en/manual/appendix/macros/supported_by_location#host-inventory) + +## Create the trigger + +> Trigger is a rule that watches for specific events, like high CPU usage. + +1. Navigate to **Configuration** > **Hosts**. +1. Go to the **Triggers** next to the host with the item you want to monitor. +1. Click **Create trigger** in top right. +- Severity: Select as needed. +- Expression: press Add + + Item: Select the item for which you want to create a trigger. + + Function: Defines how your trigger will be activated. + + Result: Actual rule of activation. + +## Testing + +1. Test manually. +- login as **zabbix** user and go to the script location. +- run your script as zabbix. + +1. Test in zabbix web interface. +- Go to your item location. +- If you are using external checks, **Test** option should be available. +- Check if you Macros give proper data to your script. +- Press **Get value and test**. + +1. Common problems. +- Timeout: you can change Timeout value in `/etc/zabbix/zabbix_server.conf` +- Make sure all necesery files and commands are accessible for zabbix user. +- If you find something else, please update the file. + +## Templates + +1. Navigate to **Configuration** > **Templates**. +1. Click **Create template** in top right. +- Specify the name, group and other information if needed. +1. Add **items**, **trigger** and **macros** if needed. +1. Link the template to **Hosts** +1. Mass update +- Go to **Hosts**. +- Check the boxes next to the desired hosts. +- Scroll to the bottom and click **Mass update**. + + Select **Link templates**. + + Choose **Link**. + + Enter your template name. + + Press **Update**. + +1. Single update +- Go to **Hosts**. + + Open desired **Host**. + + Add your Template under Templates. + + Press **Update**. diff --git a/osfv_cli/monitoring/zabbix/external_flash_probe/flash_probe.md b/osfv_cli/monitoring/zabbix/external_flash_probe/flash_probe.md new file mode 100644 index 0000000..0ed6a2e --- /dev/null +++ b/osfv_cli/monitoring/zabbix/external_flash_probe/flash_probe.md @@ -0,0 +1,97 @@ +# How To implement This script? + +1. Move `flash_probe.sh`, `flash_read.sh`, `flash_write.sh` to external script location. + +- By default it is: + +```C +/usr/lib/zabbix/externalscripts +``` + +2. Create Crontab job. + +```C +crontab -e +``` + +```C +crontab -e +``` + +> The script runs every 30 minutes between 4:00 AM and 10:00 PM, Monday to Friday. + +```C +*/30 4-22 * * 1-5 /usr/lib/zabbix/externalscripts/flash_probe.sh +``` + +## Create Template + +1. Navigate to **Configuration** > **Templates**. +2. Click **Create template** in top right. +- Specify the name, group and other information if needed. + +## Create Items + +1. First item. +- Change type to **External check**. +- Set Key to `flash_read.sh[{HOST.IP}]`. +- Set update interval to 5m. + +2. Second item. +- Change type to **External check**. +- Set Key to `flash_write.sh[{HOST.IP}]`. +- Set **update interval** to **5m**. + +## Create Triggers + +1. First trigger. +- Severity: **High** +- Name: Clip Fail +- Expression: + +```C +min(/Flash probe/flash_read_2zabbix.sh[{HOST.IP}],15m)=2 and +last(/Flash probe/flash_read_2zabbix.sh[{HOST.IP}])=2 +``` + +2. Second trigger. +- Severity: **Information** +- Name: Platform checked out +- Expression: + +```C +max(/Flash probe/flash_read_2zabbix.sh[{HOST.IP}],15m)=1 and +min(/Flash probe/flash_read_2zabbix.sh[{HOST.IP}],15m)=1 and +last(/Flash probe/flash_read_2zabbix.sh[{HOST.IP}])=1 +``` + +## Link Hosts to the template + +1. Mass update +- Go to **Hosts**. +- Check the boxes next to the desired hosts. +- Scroll to the bottom and click **Mass update**. + + Select **Link templates**. + + Choose **Link**. + + Enter your template name. + + Press **Update**. + +2. Single update. +- Go to **Hosts**. + + Open desired **Host**. + + Add your Template under Templates. + + Press **Update**. + +## Testing + +1. Test manually. +- login as **zabbix** user and go to the script location. +- run your script as zabbix. + +> You can incorporate Zabbix macros into your command, `./script.sh '192.168.10.0'`. + +2. Test in zabbix web interface. +- Go to your item location. +- If you are using external checks, **Test** option should be available. +- Check if you Macros give proper data to your script. +- Press **Get value and test**. diff --git a/osfv_cli/monitoring/zabbix/external_flash_probe/flash_probe.sh b/osfv_cli/monitoring/zabbix/external_flash_probe/flash_probe.sh new file mode 100644 index 0000000..a2dad02 --- /dev/null +++ b/osfv_cli/monitoring/zabbix/external_flash_probe/flash_probe.sh @@ -0,0 +1,61 @@ +#!/bin/bash +#Output files location +Cd /usr/lib/zabbix/externalscripts/ + +source /usr/lib/zabbix/externalscripts/venv/bin/activate + +#File with IPs to check -created by flash_write.sh +input_file="/usr/lib/zabbix/externalscripts/macros.txt" + +#The file where the IPs that passed 'flash probe' without issues are saved. +output_pass="/usr/lib/zabbix/externalscripts/output_pass.txt" +#The file where the IPs that did not pass 'flash probe' are saved. +output_fail="/usr/lib/zabbix/externalscripts/output_fail.txt" +#The file where the IPs that are currently in use are saved. +output_used="/usr/lib/zabbix/externalscripts/output_used.txt" +#The file where the IPs that are currently stolen bo this script are saved. +stolen="/usr/lib/zabbix/externalscripts/stolen.txt" + +rm "$output_used" +rm "$output_pass" +rm "$output_fail" +rm "$stolen" + +check_snipeit() { + ip=$1 + if [[ "$ip" =~ [0-9] ]]; then + if osfv_cli snipeit check_out --rte_ip "$ip" | grep -qw "successfully checked out."; then + return 0 #ip successfully checked out + else + return 3 #failed to check out + fi + else + return 2 # IP contains no numbers + fi +} + +probe_flash() { + ip=$1 + output_flash=$(osfv_cli rte --rte_ip "$ip" flash probe) + if echo "$output_flash" | grep -qE 'Found .* flash chip ".*" \(.*\) on .*\.'; then + echo "$ip" >> "$output_pass" + return 0 + else + echo "$ip" >> "$output_fail" + return 1 + fi +} + +while IFS= read -r ip || [ -n "$ip" ]; do + if check_snipeit "$ip"; then + echo "$ip successfully checked out, probing flash..." + probe_flash "$ip" + osfv_cli snipeit check_in --rte_ip "$ip" + return 0 + else + echo "Failed to checkout $ip ._." + echo "$ip" >> "$output_used" + return 3 #Failed to check out IP + fi +done < "$input_file" +deactivate diff --git a/osfv_cli/monitoring/zabbix/external_flash_probe/flash_read.sh b/osfv_cli/monitoring/zabbix/external_flash_probe/flash_read.sh new file mode 100644 index 0000000..9dde534 --- /dev/null +++ b/osfv_cli/monitoring/zabbix/external_flash_probe/flash_read.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +output_fail="/usr/lib/zabbix/externalscripts/output_fail.txt" +output_used="/usr/lib/zabbix/externalscripts/output_used.txt" +output_pass="/usr/lib/zabbix/externalscripts/output_pass.txt" + +ip=$1 + +if grep -qw "$ip" "$output_fail"; then + echo 2 +elif grep -qw "$ip" "$output_used"; then + echo 1 +elif grep -qw "$ip" "$output_pass"; then + echo 0 +fi diff --git a/osfv_cli/monitoring/zabbix/external_flash_probe/flash_write.sh b/osfv_cli/monitoring/zabbix/external_flash_probe/flash_write.sh new file mode 100644 index 0000000..0f29c9e --- /dev/null +++ b/osfv_cli/monitoring/zabbix/external_flash_probe/flash_write.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +output_file="/usr/lib/zabbix/externalscripts/macros.txt" + +touch "$output_file" + +if [ -z $1 ]; then +# echo "No macro provided." + exit 1 +fi + +macro_exists() { + grep -qF "$1" "$output_file" +} + +for macro in "$@"; do + if ! macro_exists "$macro"; then + echo "$macro" >> "$output_file" + echo 0 + else +# echo "Macro '$macro' already exists in the file." + echo 2 + fi +done diff --git a/osfv_cli/osfv_cli/osfv_cli.py b/osfv_cli/osfv_cli/osfv_cli.py index 85fac19..feab90c 100755 --- a/osfv_cli/osfv_cli/osfv_cli.py +++ b/osfv_cli/osfv_cli/osfv_cli.py @@ -72,6 +72,7 @@ def list_my_assets(snipeit_api, args): for asset in my_assets: print_asset_details(asset) + # List unused assets def list_unused_assets(snipeit_api, args): all_assets = snipeit_api.get_all_assets()