Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monitoring #36

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions osfv_cli/monitoring/zabbix/External_Check.md
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add empty line below


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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please indent

- 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**.
97 changes: 97 additions & 0 deletions osfv_cli/monitoring/zabbix/external_flash_probe/flash_probe.md
Original file line number Diff line number Diff line change
@@ -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**.
61 changes: 61 additions & 0 deletions osfv_cli/monitoring/zabbix/external_flash_probe/flash_probe.sh
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this file, and what it contains?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$input_file in this case macros.txt
contains list of IPs to use
I explained it in new commit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the purpose of this file? Can you briefly document each of them as a comment above?


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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about simply invoking check_in below? After we call probe_flash, we no longer need the device to be checked out, right? This makes another loop and stolen.txt not needed.

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
15 changes: 15 additions & 0 deletions osfv_cli/monitoring/zabbix/external_flash_probe/flash_read.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually use flash_read.sh and flash_write.sh files in monitoring process?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, flash_read.sh and flash_write.sh are executed by Zabbix and are separated into two files for ease of use with zabbix. Meanwhile flash_probe.sh is executed by a crontab or another automation tool due to Zabbix's timeout limitations.


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
Copy link
Contributor

@macpijan macpijan May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use consistent indentation

elif grep -qw "$ip" "$output_pass"; then
echo 0
fi
24 changes: 24 additions & 0 deletions osfv_cli/monitoring/zabbix/external_flash_probe/flash_write.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions osfv_cli/osfv_cli/osfv_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down