diff --git a/.gitignore b/.gitignore index 98109f1..722beb4 100755 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ spec/generated monitoring/nagios-config/ monitoring/nagios-nrpe-server-config/ generate_nagios3_config.sh~ +Gemfile.lock diff --git a/Dockerfile b/Dockerfile index 7ebf9e5..92d9a89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,6 +42,7 @@ RUN ( \ echo "fi"; \ echo "chown -R autossh:autossh /home/autossh/.ssh/"; \ echo ""; \ + echo "/app/util/install-nrpe.sh"; \ echo ""; \ echo "# Start service"; \ echo "#"; \ diff --git a/util/demographicsImporter/demographicsImporter.js b/util/demographicsImporter/demographicsImporter.js new file mode 100644 index 0000000..7a1a59d --- /dev/null +++ b/util/demographicsImporter/demographicsImporter.js @@ -0,0 +1,175 @@ +// Retrieve +var MongoClient = require('mongodb').MongoClient; +var util = require('util'); + +var preconditions = true; + +if(process.env.QUERY_TITLE === null || process.env.QUERY_TITLE === undefined) +{ + console.log('ERROR: QUERY_TITLE environment variable not set.'); +} +else +{ + console.log('QUERY_TITLE: ' + process.env.QUERY_TITLE); +} + + +if(process.env.RETRO_QUERY_TITLE === null || process.env.RETRO_QUERY_TITLE === undefined) +{ + console.log('ERROR: RETRO_QUERY_TITLE environment variable not set.'); +} +else +{ + console.log('RETRO_QUERY_TITLE: ' + process.env.RETRO_QUERY_TITLE); +} +// Connect to the db +MongoClient.connect('mongodb://localhost:27017/query_composer_development', function(err, db) { + if(err) { return console.dir(err); } + + db.collection('queries', null, + function(err, queriesCollection) + { + if(err) { throw err; } + + //fetch the retro query excution + //**************************************************************************** + queriesCollection.find({title:process.env.RETRO_QUERY_TITLE}).toArray( + function(err, retroQueries) + { + if(err) { throw err; } + + if(retroQueries.length != 1) + { + throw new Error('Not one and only one retro query: ' + process.env.RETRO_QUERY_TITLE); + } + + var retroQuery = retroQueries[0]; + + if(retroQuery.executions.length != 1) + { + throw new Error('Not one and only one execution for retro query: ' + process.env.RETRO_QUERY_TITLE); + } + + retroQuery.executions = retroQuery.executions.sort( + function(a,b) + { + return a.time > b.time ? 1 : b.time > a.time ? -1 : 0; + } + ); + + var retroQueryExecution = retroQuery.executions[0]; + + //**************************************************************************** + + //fetch query executions + //**************************************************************************** + var queries = queriesCollection.find({title:process.env.QUERY_TITLE}).toArray( + function(err, queries) + { + if(err) { throw err; } + + if(queries.length != 1) + { + throw new Error('Not one and only one query: ' + process.env.QUERY_TITLE); + } + + var query = queries[0]; + + var queryExecutions = query.executions; + //**************************************************************************** + + //fetch retro retroResults + //**************************************************************************** + var retroResults = retroQueryExecution.aggregate_result; + //**************************************************************************** + + //build simulated executions + //**************************************************************************** + var simulatedExecutions = {}; + + for( var key in retroResults) + { + if( !retroResults.hasOwnProperty(key)) + { + continue; + } + + var execution = JSON.parse(key); + + var date = execution.date/1000;//leave in milliseconds + var simulatedExecution; + + if(!execution.gender || !execution.ageRange || !execution.pid || !execution.date) + { + throw new Error('Error: Missing field'); + } + + var ar_key = execution.gender + '_' + + execution.ageRange + '_' + + execution.pid; + + if(!simulatedExecutions[date]) + { + var aggregate_result = {}; + aggregate_result[ar_key] = retroResults[key]; + simulatedExecution = {'_id':retroQueryExecution._id, + 'aggregate_result':aggregate_result, + 'notification':null, + 'time':date, + 'simulated':true + }; + simulatedExecutions[date] = simulatedExecution; + } + else + { + simulatedExecution = simulatedExecutions[date]; + simulatedExecution.aggregate_result[ar_key] = retroResults[key]; + } + } + //**************************************************************************** + + //add the simulated executions to the execution List + //**************************************************************************** + for( var se in simulatedExecutions) + { + if( !simulatedExecutions.hasOwnProperty(se)) + { + continue; + } + + queryExecutions.push( simulatedExecutions[se] ); + } + //**************************************************************************** + + //update the query with the retro results + //**************************************************************************** + + queriesCollection.updateOne({title:process.env.QUERY_TITLE}, {$set:{executions:queryExecutions}}, {upsert:true}, + function(err, result) + { + if(err) + { + throw new Error(err); + } + + db.close( + function(err, result) + { + if(err) + { + console.log('ERROR: closing db - ' + error); + } + else + { + console.log('SUCCESS'); + } + }); + }); + + //**************************************************************************** + }); + + }); + } + ); +}); diff --git a/util/nrpe/add_nrpe_command.sh b/util/nrpe/add_nrpe_command.sh new file mode 100755 index 0000000..ff8eeac --- /dev/null +++ b/util/nrpe/add_nrpe_command.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +# This script adds a command to the NRPE service, command names must be compatible with +# the command names used by the Nagios server to access the NRPE commands. +# +# This script takes 3 arguments: $ ./add_nrpe_command.sh , for example: +# +# $ ./add_nrpe_command.sh /usr/local/lib/nagios/check_processes.sh 5 /etc/nagios/nrpe_local.cfg +# +# If the PATH_TO_NRPE_CONFIG argument is not given a default /etc/nagios/nrpe_local.cfg is used. +# +# Exit Codes for the script are: +# +# 0 - Completed noramally, command succcessfully added. +# 1 - Error due to invalid parameters +# 2 - Did not complete due to command already being installed. + +if [ $# -ne 2 ] && [ $# -ne 3 ]; then + echo "" + echo "ERROR: Invalid number of parameters to script, received "$#" parameters, expected 2 or 3". + echo "Usage: $ ./add_nrpe_command.sh " + echo "" + exit 1 +fi + +CMD_SCRIPT=$1 +EP=$2 + +if [ $# == 3 ]; then + NRPE_CONFIG=$3 +else + NRPE_CONFIG=/etc/nagios/nrpe_local.cfg +fi + +# Check that all files exist as expected. + +# Check that config file is in place. +if ! [ -w $NRPE_CONFIG ]; then + echo "ERROR: no such file: $NRPE_CONFIG" + exit 1 +fi + +# Check that nrpe script file is in place. +if ! [ -x $CMD_SCRIPT ]; then + echo "ERROR: no such file: $CMD_SCRIPT" + exit 1 +fi + +# Do some manipulation of the names. +# TODO: Update this to use PDC-XXXX naming scheme; changes need to occur on Nagios server side aswell. + +SCRIPT_NAME=$(basename $CMD_SCRIPT) +EP_NUM=$(printf "%03d" $EP) +EP_NAME="pdc"$EP_NUM +CMD_NAME=${SCRIPT_NAME%.*}"_"$EP_NAME +CMD="command["$CMD_NAME"]="$CMD_SCRIPT" "$EP + +# Check if the script is already installed. + +if grep -q $CMD_NAME $NRPE_CONFIG; then + # The command is already installed, we will not overwrite this. + # + echo "WARNING: $CMD_NAME is already in the $NRPE_CONFIG file, will not overwrite." + exit 2 +else + # The command is not already installed, will append this to the config file. + # + echo $CMD >> $NRPE_CONFIG + echo "INFO: Adding $CMD to $NRPE_CONFIG file. " + exit 0 +fi diff --git a/util/nrpe/deploy_monitoring_scripts.sh b/util/nrpe/deploy_monitoring_scripts.sh new file mode 100755 index 0000000..233252b --- /dev/null +++ b/util/nrpe/deploy_monitoring_scripts.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +# This script deploys the scripts required for NRPE to monitor endpoints. +# +# !!!!!!!!!!!!!!!!! THIS SCRIPT WILL REMOVE ANY EXISTING NRPE CONFIGURATIONS !!!!!!!!!!!!!!!!!!!!!!! +# +# Assumptions: +# +# * This script assumes that the PDC composer code is deployed within the /app directory of this host. +# +# In summary this script: +# +# 1) Creates (or clears) the directory /usr/local/lib/nagios/ +# 2) Copies the monitoring scripts from ./monitoring_scripts/ into /usr/local/lib/nagios/ +# 3) Sets up commands in /etc/nagios/nrpe_local.cfg to use the scripts. +# 4) Restarts the NRPE server. +# +# This scripts takes a list of endpoint id's to deploy monitoring for example: +# +# $ deploy_monitoring_scripts.sh 1 2 3 4 5 +# +# Will set up the scripts for endpoints 1 through 5 + +echo "" +echo "" +echo "---------------------------------------------------" +echo "THIS SCRIPT WILL REMOVE ANY EXISTING NRPE CONFIGURATIONS" +read -p "Press ENTER to continue, CTRL-C to halt." +echo "---------------------------------------------------" +echo "" +echo "" + +if [ $# == 0 ]; then + echo "ERROR: This script takes endpoint number arguments, $# were provided, exiting..." + echo "" + exit 1 + +fi + +# Set up some variables + +BASE_DIR=/app/util/nrpe/ +SCRIPT_DEPLOY_DIR=/usr/local/lib/nagios +NRPE_CFG_FILE=/etc/nagios/nrpe_local.cfg + +# 1) Remove any existing scripts within the /usr/local/lib/nagios/ directory. + +rm -rf $SCRIPT_DEPLOY_DIR + +mkdir -p $SCRIPT_DEPLOY_DIR + +# 2) Copy in the default scripts from within the ./monitoring_scripts/ directory. + +cp -r $BASE_DIR""monitoring_scripts/* $SCRIPT_DEPLOY_DIR + +# 3) Set up the commands in /etc/nagios/nrpe_local.cfg, we use another script to support this. + +# 3.1) Remove existing config file + +rm -rf $NRPE_CFG_FILE +touch $NRPE_CFG_FILE + +for ep in $@ #gets all arguments to the script. +do + echo "" + echo "Configuring scripts for endpoint: "$ep + echo "---------------------------------" + + for f in $SCRIPT_DEPLOY_DIR/* #get all executable filese + do + if [[ -x $f ]]; then + ./add_nrpe_command.sh $f $ep $NRPE_CFG_FILE + fi + done + echo "================================" +done + +# 4) Restart nagios-nrpe server + +echo "" +echo "Restarting NRPE Server: " +echo "------------------------" + +sudo service nagios-nrpe-server restart + +echo "" +echo "-----------------------" +echo "All Done, Exiting..." + +exit 0 diff --git a/util/nrpe/endpoints.txt b/util/nrpe/endpoints.txt new file mode 100644 index 0000000..93a4845 --- /dev/null +++ b/util/nrpe/endpoints.txt @@ -0,0 +1 @@ +1 2 3 4 diff --git a/util/nrpe/install-nrpe.sh b/util/nrpe/install-nrpe.sh new file mode 100755 index 0000000..028d8c4 --- /dev/null +++ b/util/nrpe/install-nrpe.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +PWD=$(pwd) + +NRPE_PORT=3010 +NRPE_CONFIG=/etc/nagios/nrpe.cfg + +# install the NRPE service + +sudo apt-get update +sudo apt-get install nagios-nrpe-plugin -y + +# Set the port for NRPE to listen on: + +sed -i 's/server_port=[0-9]\+/server_port='$NRPE_PORT'/g' $NRPE_CONFIG + +# Make the /usr/local/lib/nagios directory for storing plugin scripts + +mkdir -p /usr/local/lib/nagios + +# Make a nrpe_local.cfg file if it does not already exist + +touch /etc/nagios/nrpe_local.cfg + +# Move into the working directory + +cd /app/util/nrpe/ + +# Use the file endpoints.txt as a list of endpoint numbers to use. + +./deploy_monitoring_scripts.sh $(cat endpoints.txt) + +# Move back to original directory + +cd $PWD diff --git a/util/nrpe/monitoring_scripts/README.md b/util/nrpe/monitoring_scripts/README.md new file mode 100644 index 0000000..583dfc8 --- /dev/null +++ b/util/nrpe/monitoring_scripts/README.md @@ -0,0 +1,11 @@ +## NRPE Monitoring Scripts + +Each of these scripts makes a call to the endpoint to request information about that system's health. + +Each script should take exactly 1 argument and must be executable, the endpont number make the request to, for example: + +`./check_processes.sh 4` + +Will check the number of processes running on the PDC-0004 endpoint. + +Any modications to this paradigm (1 argument) will require adjustments to the scripts that manage this scripts, in the parent directory. diff --git a/util/nrpe/monitoring_scripts/check_delay_job.sh b/util/nrpe/monitoring_scripts/check_delay_job.sh new file mode 100755 index 0000000..13c89d5 --- /dev/null +++ b/util/nrpe/monitoring_scripts/check_delay_job.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# This script takes 1 argument, the endpoint number to check. + +# Check the number of arguments and make sure it is as expected. +if [ $# -ne 1 ]; then + echo "" + echo "ERROR: Script takes exactly 1 argument, the endpoint to connect to." + echo "Sample usage: $ ./check_delayed_job 5" + echo "" + exit 1; +fi + +# Get the port by adding the endpoint number base port. +PORT=$((40000+$1)) + +URL="http://localhost:"$PORT"/sysinfo/delayed_job" +TMPFILE=`/bin/tempfile -p_PDC_` +CMD=`/usr/bin/curl -sSf $URL > $TMPFILE 2>&1` +STATUS=$? +if [ $STATUS -ne 0 ]; then + /bin/echo "UNKNOWN - For $URL got $(cat $TMPFILE)" + /bin/rm $TMPFILE + exit 3 +fi + +# Expect two lines of output from call to web server + +# first line should be Nagios message +NAGIOSMSG=`/usr/bin/head -1 $TMPFILE` +# second line should be Nagios status code +STATUSSTR=`/bin/cat $TMPFILE | /usr/bin/head -2 | /usr/bin/tail -1` +/bin/rm $TMPFILE +NAGIOSSTATUS=-1 +if [ "$STATUSSTR" == "Status Code: 0" ]; then + NAGIOSSTATUS=0 +fi +if [ "$STATUSSTR" == "Status Code: 1" ]; then + NAGIOSSTATUS=1 +fi +if [ "$STATUSSTR" == "Status Code: 2" ]; then + NAGIOSSTATUS=2 +fi +if [ "$STATUSSTR" == "Status Code: 3" ]; then + NAGIOSSTATUS=3 +fi +if [ "$NAGIOSSTATUS" -ge 0 -a "$NAGIOSSTATUS" -le 3 ]; then + echo $NAGIOSMSG + exit $NAGIOSSTATUS +else + # shouldn't get here unless there was a completely unexpected response + /bin/echo "UNKNOWN - Unexpected response from \"$URL\". Return status $STATUS" + exit 3 +fi diff --git a/util/nrpe/monitoring_scripts/check_diskspace.sh b/util/nrpe/monitoring_scripts/check_diskspace.sh new file mode 100755 index 0000000..20aa5b9 --- /dev/null +++ b/util/nrpe/monitoring_scripts/check_diskspace.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# This script takes 1 argument, the endpoint number to check. + +# Check the number of arguments and make sure it is as expected. + +if [ $# -ne 1 ]; then + echo "" + echo "ERROR: Script takes exactly 1 argument, the endpoint to connect to." + echo "Sample usage: $ ./check_diskspace.sh 5" + echo "" + exit 1; +fi + +# Get the port by adding the endpoint number base port. +PORT=$((40000+$1)) + +URL="http://localhost:"$PORT"/sysinfo/diskspace" +TMPFILE=`/bin/tempfile -p_PDC_` +CMD=`/usr/bin/curl -sSf $URL > $TMPFILE 2>&1` +STATUS=$? +if [ $STATUS -ne 0 ]; then + /bin/echo "UNKNOWN - For $URL got $(cat $TMPFILE)" + /bin/rm $TMPFILE + exit 3 +fi + +# Expect two lines of output from call to web server + +# first line should be Nagios message +NAGIOSMSG=`/usr/bin/head -1 $TMPFILE` +# second line should be Nagios status code +STATUSSTR=`/bin/cat $TMPFILE | /usr/bin/head -2 | /usr/bin/tail -1` +/bin/rm $TMPFILE +NAGIOSSTATUS=-1 +if [ "$STATUSSTR" == "Status Code: 0" ]; then + NAGIOSSTATUS=0 +fi +if [ "$STATUSSTR" == "Status Code: 1" ]; then + NAGIOSSTATUS=1 +fi +if [ "$STATUSSTR" == "Status Code: 2" ]; then + NAGIOSSTATUS=2 +fi +if [ "$STATUSSTR" == "Status Code: 3" ]; then + NAGIOSSTATUS=3 +fi +if [ "$NAGIOSSTATUS" -ge 0 -a "$NAGIOSSTATUS" -le 3 ]; then + echo $NAGIOSMSG + exit $NAGIOSSTATUS +else + # shouldn't get here unless there was a completely unexpected response + /bin/echo "UNKNOWN - Unexpected response from \"$URL\". Return status $STATUS" + exit 3 +fi diff --git a/util/nrpe/monitoring_scripts/check_import.sh b/util/nrpe/monitoring_scripts/check_import.sh new file mode 100755 index 0000000..dc9cb41 --- /dev/null +++ b/util/nrpe/monitoring_scripts/check_import.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# This script takes 1 argument, the endpoint number to check. + +# Check the number of arguments and make sure it is as expected. + +if [ $# -ne 1 ]; then + echo "" + echo "ERROR: Script takes exactly 1 argument, the endpoint to connect to." + echo "Sample usage: $ ./check_import.sh 5" + echo "" + exit 1 +fi + +# Get the port by adding the endpoint number base port. +PORT=$((40000+$1)) + +URL="http://localhost:"$PORT"/sysinfo/import" + +TMPFILE=`/bin/tempfile -p_PDC_` + +CMD=`/usr/bin/curl -sSf $URL > $TMPFILE 2>&1` + +STATUS=$? + +if [ $STATUS -ne 0 ]; then + + /bin/echo "UNKNOWN - For $URL got $(cat $TMPFILE)" + /bin/rm $TMPFILE + exit 3 + +fi + +# Expect two lines of output from call to web server + +# first line should be Nagios message +NAGIOSMSG=`/usr/bin/head -1 $TMPFILE` +# second line should be Nagios status code +STATUSSTR=`/bin/cat $TMPFILE | /usr/bin/head -2 | /usr/bin/tail -1` +/bin/rm $TMPFILE +NAGIOSSTATUS=-1 +if [ "$STATUSSTR" == "Status Code: 0" ]; then + NAGIOSSTATUS=0 +fi +if [ "$STATUSSTR" == "Status Code: 1" ]; then + NAGIOSSTATUS=1 +fi +if [ "$STATUSSTR" == "Status Code: 2" ]; then + NAGIOSSTATUS=2 +fi +if [ "$STATUSSTR" == "Status Code: 3" ]; then + NAGIOSSTATUS=3 +fi +if [ "$NAGIOSSTATUS" -ge 0 -a "$NAGIOSSTATUS" -le 3 ]; then + echo $NAGIOSMSG + exit $NAGIOSSTATUS +else + # shouldn't get here unless there was a completely unexpected response + /bin/echo "UNKNOWN - Unexpected response from \"$URL\". Return status $STATUS" + exit 3 +fi diff --git a/util/nrpe/monitoring_scripts/check_load.sh b/util/nrpe/monitoring_scripts/check_load.sh new file mode 100755 index 0000000..108fbc2 --- /dev/null +++ b/util/nrpe/monitoring_scripts/check_load.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# This script takes 1 argument, the endpoint number to check. + +# Check the number of arguments and make sure it is as expected. + +if [ $# -ne 1 ]; then + echo "" + echo "ERROR: Script takes exactly 1 argument, the endpoint to connect to." + echo "Sample usage: $ ./check_load.sh 5" + echo "" + exit 1; +fi + +# Get the port by adding the endpoint number base port. +PORT=$((40000+$1)) + +URL="http://localhost:"$PORT"/sysinfo/load" +TMPFILE=`/bin/tempfile -p_PDC_` +CMD=`/usr/bin/curl -sSf $URL > $TMPFILE 2>&1` +STATUS=$? + +if [ $STATUS -ne 0 ]; then + /bin/echo "UNKNOWN - For $URL got $(cat $TMPFILE)" + /bin/rm $TMPFILE + exit 3 +fi + +# Expect two lines of output from call to web server + +# first line should be Nagios message +NAGIOSMSG=`/usr/bin/head -1 $TMPFILE` +# second line should be Nagios status code +STATUSSTR=`/bin/cat $TMPFILE | /usr/bin/head -2 | /usr/bin/tail -1` +/bin/rm $TMPFILE +NAGIOSSTATUS=-1 +if [ "$STATUSSTR" == "Status Code: 0" ]; then + NAGIOSSTATUS=0 +fi +if [ "$STATUSSTR" == "Status Code: 1" ]; then + NAGIOSSTATUS=1 +fi +if [ "$STATUSSTR" == "Status Code: 2" ]; then + NAGIOSSTATUS=2 +fi +if [ "$STATUSSTR" == "Status Code: 3" ]; then + NAGIOSSTATUS=3 +fi +if [ "$NAGIOSSTATUS" -ge 0 -a "$NAGIOSSTATUS" -le 3 ]; then + echo $NAGIOSMSG + exit $NAGIOSSTATUS +else + # shouldn't get here unless there was a completely unexpected response + /bin/echo "UNKNOWN - Unexpected response from \"$URL\". Return status $STATUS" + exit 3 +fi diff --git a/util/nrpe/monitoring_scripts/check_processes.sh b/util/nrpe/monitoring_scripts/check_processes.sh new file mode 100755 index 0000000..9e5e4e3 --- /dev/null +++ b/util/nrpe/monitoring_scripts/check_processes.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# This script takes 1 argument, the endpoint number to check. + +# Check the number of arguments and make sure it is as expected. +if [ $# -ne 1 ]; then + echo "" + echo "ERROR: Script takes exactly 1 argument, the endpoint to connect to." + echo "Sample usage: $ ./check_processes.sh 5" + echo "" + exit 1; +fi + + +# Get the port by adding the endpoint number base port. +PORT=$((40000+$1)) + +URL="http://localhost:"$PORT"/sysinfo/processes" +TMPFILE=`/bin/tempfile` + +# Expect two lines of output from call to web server +CMD=`/usr/bin/curl -sSf $URL > $TMPFILE 2>&1` +STATUS=$? +if [ $STATUS -ne 0 ]; then + /bin/echo "UNKNOWN - For $URL got $(cat $TMPFILE)" + /bin/rm $TMPFILE + exit 3 +fi +# first line should be Nagios message +NAGIOSMSG=`/usr/bin/head -1 $TMPFILE` +# second line should be Nagios status code +STATUSSTR=`/bin/cat $TMPFILE | /usr/bin/head -2 | /usr/bin/tail -1` +/bin/rm $TMPFILE +NAGIOSSTATUS=-1 +if [ "$STATUSSTR" == "Status Code: 0" ]; then + NAGIOSSTATUS=0 +fi +if [ "$STATUSSTR" == "Status Code: 1" ]; then + NAGIOSSTATUS=1 +fi +if [ "$STATUSSTR" == "Status Code: 2" ]; then + NAGIOSSTATUS=2 +fi +if [ "$STATUSSTR" == "Status Code: 3" ]; then + NAGIOSSTATUS=3 +fi +if [ "$NAGIOSSTATUS" -ge 0 -a "$NAGIOSSTATUS" -le 3 ]; then + echo $NAGIOSMSG + exit $NAGIOSSTATUS +else + # shouldn't get here unless there was a completely unexpected response + /bin/echo "UNKNOWN - Unexpected response from \"$URL\". Return status $STATUS" + exit 3 +fi diff --git a/util/nrpe/monitoring_scripts/check_swap.sh b/util/nrpe/monitoring_scripts/check_swap.sh new file mode 100755 index 0000000..c04671e --- /dev/null +++ b/util/nrpe/monitoring_scripts/check_swap.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# This script takes 1 argument, the endpoint number to check. + +# Check the number of arguments and make sure it is as expected. + +if [ $# -ne 1 ]; then + echo "" + echo "ERROR: Script takes exactly 1 argument, the endpoint to connect to." + echo "Sample usage: $ ./check_swap.sh 5" + echo "" + exit 1 +fi + +# Get the port by adding the endpoint number base port. +PORT=$((40000+$1)) + +URL="http://localhost:"$PORT"/sysinfo/swap" +TMPFILE=`/bin/tempfile -p_PDC_` +CMD=`/usr/bin/curl -sSf $URL > $TMPFILE 2>&1` +STATUS=$? + +if [ $STATUS -ne 0 ]; then + /bin/echo "UNKNOWN - For $URL got $(cat $TMPFILE)" + /bin/rm $TMPFILE + exit 3 +fi + +# Expect two lines of output from call to web server +# first line should be Nagios message +NAGIOSMSG=`/usr/bin/head -1 $TMPFILE` +# second line should be Nagios status code +STATUSSTR=`/bin/cat $TMPFILE | /usr/bin/head -2 | /usr/bin/tail -1` +/bin/rm $TMPFILE +NAGIOSSTATUS=-1 +if [ "$STATUSSTR" == "Status Code: 0" ]; then + NAGIOSSTATUS=0 +fi +if [ "$STATUSSTR" == "Status Code: 1" ]; then + NAGIOSSTATUS=1 +fi +if [ "$STATUSSTR" == "Status Code: 2" ]; then + NAGIOSSTATUS=2 +fi +if [ "$STATUSSTR" == "Status Code: 3" ]; then + NAGIOSSTATUS=3 +fi +if [ "$NAGIOSSTATUS" -ge 0 -a "$NAGIOSSTATUS" -le 3 ]; then + echo $NAGIOSMSG + exit $NAGIOSSTATUS +else + # shouldn't get here unless there was a completely unexpected response + /bin/echo "UNKNOWN - Unexpected response from \"$URL\". Return status $STATUS" + exit 3 +fi diff --git a/util/nrpe/monitoring_scripts/check_tomcat.sh b/util/nrpe/monitoring_scripts/check_tomcat.sh new file mode 100755 index 0000000..bfaf375 --- /dev/null +++ b/util/nrpe/monitoring_scripts/check_tomcat.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# This script takes 1 argument, the endpoint number to check. + +# Check the number of arguments and make sure it is as expected. + +if [ $# -ne 1 ]; then + echo "" + echo "ERROR: Script takes exactly 1 argument, the endpoint to connect to." + echo "Sample usage: $ ./check_tomcat.sh 5" + echo "" + exit 1 +fi + +# Get the port by adding the endpoint number base port. +PORT=$((40000+$1)) + +URL="http://localhost:"$PORT"/sysinfo/tomcat" + +TMPFILE=`/bin/tempfile -p_PDC_` +CMD=`/usr/bin/curl -sSf $URL > $TMPFILE 2>&1` +STATUS=$? + +if [ $STATUS -ne 0 ]; then + /bin/echo "UNKNOWN - For $URL got $(cat $TMPFILE)" + /bin/rm $TMPFILE + exit 3 +fi + +# Expect two lines of output from call to web server +# first line should be Nagios message +NAGIOSMSG=`/usr/bin/head -1 $TMPFILE` +# second line should be Nagios status code +STATUSSTR=`/bin/cat $TMPFILE | /usr/bin/head -2 | /usr/bin/tail -1` +/bin/rm $TMPFILE +NAGIOSSTATUS=-1 +if [ "$STATUSSTR" == "Status Code: 0" ]; then + NAGIOSSTATUS=0 +fi +if [ "$STATUSSTR" == "Status Code: 1" ]; then + NAGIOSSTATUS=1 +fi +if [ "$STATUSSTR" == "Status Code: 2" ]; then + NAGIOSSTATUS=2 +fi +if [ "$STATUSSTR" == "Status Code: 3" ]; then + NAGIOSSTATUS=3 +fi +if [ "$NAGIOSSTATUS" -ge 0 -a "$NAGIOSSTATUS" -le 3 ]; then + echo $NAGIOSMSG + exit $NAGIOSSTATUS +else + # shouldn't get here unless there was a completely unexpected response + /bin/echo "UNKNOWN - Unexpected response from \"$URL\". Return status $STATUS" + exit 3 +fi diff --git a/util/nrpe/monitoring_scripts/check_users.sh b/util/nrpe/monitoring_scripts/check_users.sh new file mode 100755 index 0000000..326fa3f --- /dev/null +++ b/util/nrpe/monitoring_scripts/check_users.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# This script takes 1 argument, the endpoint number to check. + +# Check the number of arguments and make sure it is as expected. + +if [ $# -ne 1 ]; then + echo "" + echo "ERROR: Script takes exactly 1 argument, the endpoint to connect to." + echo "Sample usage: $ ./check_users.sh 5" + echo "" + exit 1 +fi + +# Get the port by adding the endpoint number base port. +PORT=$((40000+$1)) + +URL="http://localhost:"$PORT"/sysinfo/users" + +TMPFILE=`/bin/tempfile -p_PDC_` +CMD=`/usr/bin/curl -sSf $URL > $TMPFILE 2>&1` +STATUS=$? + +if [ $STATUS -ne 0 ]; then + /bin/echo "UNKNOWN - For $URL got $(cat $TMPFILE)" + /bin/rm $TMPFILE + exit 3 +fi +# Expect two lines of output from call to web server +# first line should be Nagios message +NAGIOSMSG=`/usr/bin/head -1 $TMPFILE` +# second line should be Nagios status code +STATUSSTR=`/bin/cat $TMPFILE | /usr/bin/head -2 | /usr/bin/tail -1` +/bin/rm $TMPFILE +NAGIOSSTATUS=-1 +if [ "$STATUSSTR" == "Status Code: 0" ]; then + NAGIOSSTATUS=0 +fi +if [ "$STATUSSTR" == "Status Code: 1" ]; then + NAGIOSSTATUS=1 +fi +if [ "$STATUSSTR" == "Status Code: 2" ]; then + NAGIOSSTATUS=2 +fi +if [ "$STATUSSTR" == "Status Code: 3" ]; then + NAGIOSSTATUS=3 +fi +if [ "$NAGIOSSTATUS" -ge 0 -a "$NAGIOSSTATUS" -le 3 ]; then + echo $NAGIOSMSG + exit $NAGIOSSTATUS +else + # shouldn't get here unless there was a completely unexpected response + /bin/echo "UNKNOWN - Unexpected response from \"$URL\". Return status $STATUS" + exit 3 +fi diff --git a/util/retroImporter/retroImporter.js b/util/retroImporter/retroImporter.js new file mode 100644 index 0000000..9ac0f34 --- /dev/null +++ b/util/retroImporter/retroImporter.js @@ -0,0 +1,252 @@ +// Retrieve +var MongoClient = require('mongodb').MongoClient; +var util = require('util'); + +var preconditions = true; + +if(process.env.QUERY_TITLE === null || process.env.QUERY_TITLE === undefined) +{ + console.log('ERROR: QUERY_TITLE environment variable not set.'); +} +else +{ + console.log('QUERY_TITLE: ' + process.env.QUERY_TITLE); +} + + +if(process.env.RETRO_QUERY_TITLE === null || process.env.RETRO_QUERY_TITLE === undefined) +{ + console.log('ERROR: RETRO_QUERY_TITLE environment variable not set.'); +} +else +{ + console.log('RETRO_QUERY_TITLE: ' + process.env.RETRO_QUERY_TITLE); +} +// Connect to the db +MongoClient.connect('mongodb://hubdb:27017/query_composer_development', function(err, db) { + if(err) { return console.dir(err); } + + db.collection('queries', null, + function(err, queriesCollection) + { + if(err) { throw err; } + + //fetch the retro query excution + //**************************************************************************** + queriesCollection.find({title:process.env.RETRO_QUERY_TITLE}).toArray( + function(err, retroQueries) + { + if(err) { throw err; } + + if(retroQueries.length != 1) + { + throw new Error('Not one and only one retro query: ' + process.env.RETRO_QUERY_TITLE); + } + + var retroQuery = retroQueries[0]; + + if(retroQuery.executions.length != 1) + { + throw new Error('Not one and only one execution for retro query: ' + process.env.RETRO_QUERY_TITLE); + } + + retroQuery.executions = retroQuery.executions.sort( + function(a,b) + { + return a.time > b.time ? 1 : b.time > a.time ? -1 : 0; + } + ); + + var retroQueryExecution = retroQuery.executions[0]; + + //**************************************************************************** + + //fetch query executions + //**************************************************************************** + var queries = queriesCollection.find({title:process.env.QUERY_TITLE}).toArray( + function(err, queries) + { + if(err) { throw err; } + + if(queries.length != 1) + { + throw new Error('Not one and only one query: ' + process.env.QUERY_TITLE); + } + + var query = queries[0]; + + var queryExecutions = query.executions; + //**************************************************************************** + + //fetch retro retroResults + //**************************************************************************** + var retroResults = retroQueryExecution.aggregate_result; + //**************************************************************************** + + //build simulated executions + //**************************************************************************** + var simulatedExecutions = {}; + + for( var key in retroResults) + { + if( !retroResults.hasOwnProperty(key)) + { + continue; + } + + var execution = JSON.parse(key); + + var date = execution.date/1000;//convert to seconds + var simulatedExecution; + + if( !simulatedExecutions[date] ) + { + var aggregate_result = {}; + var ar_key; + + if(execution.value === 'numerator') + { + ar_key = 'numerator_' + execution.pid; + aggregate_result[ar_key] = retroResults[key]; + } + else if(execution.value === 'denominator') + { + ar_key = 'denominator_' + execution.pid; + aggregate_result[ar_key] = retroResults[key]; + } + else { + throw new Error('key did not have value in ["numerator", "denominator"]'); + } + + simulatedExecution = {'_id':retroQueryExecution._id, + 'aggregate_result':aggregate_result, + 'notification':null, + 'time':date + }; + + simulatedExecutions[date] = simulatedExecution; + } + else + { + simulatedExecution = simulatedExecutions[date]; + console.log("simulatedExecution:") + console.log(simulatedExecution); + console.log("execution:") + console.log(execution); + if(!simulatedExecution) + { + throw new Error('simulatedExecution evaluted to false'); + } + + var FIELD = 0; + var PID = 1; + + function getPIDMatch(simulatedExecution, execution) + { + var keys = Object.keys(simulatedExecution.aggregate_result); + + + for( var key in keys) + { + if(!keys.hasOwnProperty(key)) + { + continue; + } + + + var attributes = keys[key].split('_'); + + var pid = attributes[PID]; + //var field_test = attributes[FIELD]; + + if(execution.pid === pid) + { + + return attributes; + } + + console.log("execution.pid: "+ execution.pid); + console.log("pid: "+ pid); + } + return null; + + } + + var pidMatch = getPIDMatch(simulatedExecution, execution); + + if(pidMatch !== null) + { + + console.log("pidMatch: "+ pidMatch); + console.log("execution.value: "+ execution.value); + + if(execution.value==='denominator') + { + simulatedExecution.aggregate_result['denominator_' + execution.pid] = retroResults[key]; + } + else if(execution.value === 'numerator') + { + simulatedExecution.aggregate_result['numerator_' + execution.pid] = retroResults[key]; + } + else + { + console.log(simulatedExecutions[date]); + throw new Error("ERROR: no match for date: " + date); + } + } + else + { + simulatedExecution.aggregate_result[execution.value + '_' + execution.pid] = retroResults[key]; + } + + + simulatedExecution.aggregate_result.simulated = true; + } + } + //**************************************************************************** + + //add the simulated executions to the execution List + //**************************************************************************** + for( var se in simulatedExecutions) + { + if( !simulatedExecutions.hasOwnProperty(se)) + { + continue; + } + + queryExecutions.push( simulatedExecutions[se] ); + } + //**************************************************************************** + + //update the query with the retro results + //**************************************************************************** + + queriesCollection.updateOne({title:process.env.QUERY_TITLE}, {$set:{executions:queryExecutions}}, {upsert:true}, + function(err, result) + { + if(err) + { + throw new Error(err); + } + + db.close( + function(err, result) + { + if(err) + { + console.log('ERROR: closing db - ' + error); + } + else + { + console.log('SUCCESS'); + } + }); + }); + + //**************************************************************************** + }); + + }); + } + ); +});