#!/bin/bash function help() { cat; }</dev/null 2>&1 && pwd) plot_file="$script_path/pinger.gp" log_file="" # leave empty for stdout # defaults default_csv="$script_path/../data/ping.csv" default_ymax=250 default_start_date=$(date --date="1 week ago" -Is) default_end_date=$(date -Is) default_hours=24 default_tz=$(date +%-:::z) # end configuration ############################## # variables declare -A plot_env #functions function log() { local time=$(date +%F_%T) local prefix="LOG" case $1 in ERR | ERROR) prefix="ERR" shift ;; WAR | WARNING) prefix="WAR" shift ;; esac local func=$1; shift # remove any \n #local text=$(echo "$@"| tr '\n' '#' | sed 's|#|\\n |g') case "$log_file" in "") echo "[$time]:[$prefix]:[$0][$func]: $@" 2>&1 ;; *) echo "[$time]:[$prefix]:[$0][$func]: $@" >> $log_file ;; esac } function date_to_epoch() { echo $(date --date="$1" +"%s") } function load_server_env() { if [ -f "$script_path/../servers.env" ]; then echo $(cat "$script_path/../servers.env" 2>/dev/null) | sed 's|\s|;|g' elif [ -f "$script_path/servers.env" ]; then echo $(cat "$script_path/servers.env" 2>/dev/null) | sed 's|\s|;|g' else echo '' fi } # set X_MIN relative to X_MAX is set function set_relative_X_MIN() { local hours=$1 if [[ -z "${plot_env[X_MAX]}" ]]; then plot_env[X_MIN]=$(date_to_epoch $(date --date="$hours hours ago" -Is)) else plot_env[X_MIN]=$(( ${plot_env[X_MAX]} - $hours * 3600 )) fi } function create_env() { local env="" for env_var in "${!plot_env[@]}"; do env+=" -e $env_var=${plot_env[$env_var]}" done echo "$env" } main() { local unknown_args=() local servers=$(load_server_env) local csv_file="$default_csv" limit time_zone="$default_tz" start_date end_date hours # if servers passed as args reset list and parse [[ $# -gt 0 ]] && [[ $1 != -* ]] && servers="" while [[ $# -gt 0 ]] && [[ $1 != -* ]]; do servers="$servers;$1" shift done # parse args while [[ $# -gt 0 ]]; do key="$1" case $key in --file|-f) if [[ $2 == -* ]] || [[ -z "$2" ]]; then echo "log WAR main Invalid csv file, using default: $csv_file" shift # past argument, no value else csv_file=$2 shift # past argument shift # past value fi ;; --limit-y|-l) if [[ $2 == -* ]] || [[ -z "$2" ]]; then limit=$default_ymax shift # past argument, no value else limit=$2 shift # past argument shift # past value fi ;; --time-zone|-t) if [[ $2 == -* ]] || [[ -z "$2" ]]; then time_zone='' shift # past argument, no value else time_zone=$2 shift # past argument shift # past value fi ;; --start-date|-S) if [[ $2 == -* ]] || [[ -z "$2" ]]; then start_date=$(date_to_epoch $default_start_date) shift # past argument, no value else start_date=$(date_to_epoch "$2") shift # past argument shift # past value fi ;; --end-date|-E) if [[ $2 == -* ]] || [[ -z "$2" ]]; then end_date=$(date_to_epoch $default_end_date) shift # past argument, no value else end_date=$(date_to_epoch "$2") shift # past argument shift # past value fi ;; --show-hours|-H) if [[ $2 == -* ]] || [[ -z "$2" ]]; then hours=$default_hours shift # past argument, no value else hours=$2 shift # past argument shift # past value fi ;; --help|-h) help exit ;; *) # unknown option unknown_args+=("$1") # save it in an array for later shift # past argument ;; esac done # exit with error if no servers to plot if [[ -z "$servers" ]]; then log ERR main no servers to plot exit 1 fi # assign env in order to set relative X_MIN [[ ! -z "$servers" ]] && plot_env[SERVERS]=\"$servers\" [[ ! -z "$csv_file" ]] && plot_env[CSV_FILE]=\"$csv_file\" [[ ! -z "$limit" ]] && plot_env[Y_MAX]=$limit [[ ! -z "$time_zone" ]] && plot_env[TZ]=$time_zone [[ ! -z "$start_date" ]] && plot_env[X_MIN]=$start_date [[ ! -z "$end_date" ]] && plot_env[X_MAX]=$end_date [[ ! -z "$hours" ]] && set_relative_X_MIN $hours [[ ${#unknown_args[@]} -ne 0 ]] && log main WARNING "ignored unknown args: ${unknown_args[@]}" log main "plot ARGS: $(create_env)" gnuplot $(create_env) -p $plot_file } main "$@"