Sample Shell plugin script with jq library

Sample Shell plugin script with jq library


#!/bin/bash

# Pre-requisites: jq library to run
# ubuntu: sudo apt-get install jq

##### Site24x7 Configuration Constants #####
PLUGIN_VERSION=1
HEARTBEAT_REQUIRED=true

##### Plugin Input Constants #####
DIRNAME=.
SEARCH_LEVEL=1

##### Plugin Code #####
DATA='{}'

DATA=$( echo $DATA | jq '. + {"plugin_version": "'$PLUGIN_VERSION'"}') 
DATA=$( echo $DATA | jq '. + {"heartbeat_required": "'$HEARTBEAT_REQUIRED'"}') 

##### Plugin Logic to get the number of files and directories
FILES=`find $DIRNAME -maxdepth $SEARCH_LEVEL -type f | wc -l`
DIRS=`find $DIRNAME/* -maxdepth $SEARCH_LEVEL -type d | wc -l`


##### Add Key and value to be monitored
##### Replace the sample keys with the metrics you want to monitor
##### Execute the commands and store the values in a variable
##### Replace the dummy values with the variables to monitor
DATA=$( echo $DATA | jq '. + {"file_count": '$FILES'}') 
DATA=$( echo $DATA | jq '. + {"dir_count": '$DIRS'}') 

##### Add the units for the values to be monitored #####
DATA=$( echo $DATA | jq '. + {"units": {} }') 

##### Remove if not required
DATA=$( echo $DATA | jq '.units.file_count = "count"') 
DATA=$( echo $DATA | jq '.units.dir_count = "count"') 

##### Print the data
echo $DATA  | jq '.'