From d30bf3403e572ab738b4e5f42983cff7bc3d38be Mon Sep 17 00:00:00 2001 From: "C. Thomas Tyler" Date: Sat, 6 Aug 2022 18:41:08 -0400 Subject: [PATCH 1/9] chmod +x; no code change. --- scripts/check_for_updates.sh | 0 scripts/push_metrics.sh | 0 scripts/test_monitor_metrics.py | 0 scripts/update_p4prom.sh | 0 4 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 scripts/check_for_updates.sh mode change 100644 => 100755 scripts/push_metrics.sh mode change 100644 => 100755 scripts/test_monitor_metrics.py mode change 100644 => 100755 scripts/update_p4prom.sh diff --git a/scripts/check_for_updates.sh b/scripts/check_for_updates.sh old mode 100644 new mode 100755 diff --git a/scripts/push_metrics.sh b/scripts/push_metrics.sh old mode 100644 new mode 100755 diff --git a/scripts/test_monitor_metrics.py b/scripts/test_monitor_metrics.py old mode 100644 new mode 100755 diff --git a/scripts/update_p4prom.sh b/scripts/update_p4prom.sh old mode 100644 new mode 100755 From b3fe874f0aa63a53308d90576eea5e6f1127f934 Mon Sep 17 00:00:00 2001 From: "C. Thomas Tyler" Date: Sat, 6 Aug 2022 23:20:59 -0400 Subject: [PATCH 2/9] ShellCheck for monitor_metrics.sh, functionally similar with optimizations and bug fixes. --- scripts/monitor_metrics.sh | 383 ++++++++++++++++++++++--------------- 1 file changed, 225 insertions(+), 158 deletions(-) diff --git a/scripts/monitor_metrics.sh b/scripts/monitor_metrics.sh index 3daf7f7..d83d258 100755 --- a/scripts/monitor_metrics.sh +++ b/scripts/monitor_metrics.sh @@ -15,7 +15,7 @@ # Note we use a tempfile for each metric to avoid partial reads. Textfile collector only looks for files # ending in .prom so we do a final rename when ready -if [[ -z "${BASH_VERSINFO}" ]] || [[ -z "${BASH_VERSINFO[0]}" ]] || [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then +if [[ -z "${BASH_VERSINFO[0]}" ]] || [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then echo "This script requires Bash version >= 4"; exit 1; fi @@ -37,7 +37,7 @@ data_file=/p4/monitor_metrics.dat # ============================================================ function msg () { echo -e "$*"; } -function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; } +function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; } function usage { @@ -56,6 +56,10 @@ monitor_metrics.sh [ | -nosdp [-p ] | [-u ] ] | [-m "$tmp_info_data" - [[ $? -eq 0 ]] || bail "Can't connect to P4PORT: $P4PORT" + $p4 info -s > "$tmp_info_data" || bail "Can't connect to P4PORT: $P4PORT" sdpinst_label=",sdpinst=\"$SDP_INSTANCE\"" sdpinst_suffix="-$SDP_INSTANCE" p4logfile="$P4LOG" @@ -116,14 +120,13 @@ else p4port=${Port:-$P4PORT} p4user=${User:-$P4USER} p4="p4 -u $p4user -p $p4port" - $p4 info -s > "$tmp_info_data" - [[ $? -eq 0 ]] || bail "Can't connect to P4PORT: $p4port" + $p4 info -s > "$tmp_info_data" || bail "Can't connect to P4PORT: $p4port" sdpinst_label="" sdpinst_suffix="" tmp_config_data="$metrics_root/tmp_config_show.dat" $p4 configure show > "$tmp_config_data" p4logfile=$(grep P4LOG "$tmp_config_data" | sed -e 's/P4LOG=//' -e 's/ .*//') - errors_file=$(egrep "serverlog.file.*errors.csv" "$tmp_config_data" | cut -d= -f2 | sed -e 's/ (.*//') + errors_file=$(grep -E "serverlog.file.*errors.csv" "$tmp_config_data" | cut -d= -f2 | sed -e 's/ (.*//') check_for_replica=$(grep -c 'Replica of:' "$tmp_info_data") if [[ "$check_for_replica" -eq "0" ]]; then P4REPLICA="FALSE" @@ -159,7 +162,7 @@ load_data_file () { [[ -f "$data_file" ]] || { echo "data_file not found"; return ; } # Read in the data file information - while read filetype mod_time line_count + while read -r filetype mod_time line_count do case "$filetype" in 'p4log') @@ -171,7 +174,7 @@ load_data_file () { p4err_lc_last=$line_count ;; esac - done < $data_file + done < "$data_file" echo "data file loaded:" echo "[p4log][last timestamp: ${p4log_ts_last}][last linecount: ${p4log_lc_last}]" echo "[p4err][last timestamp: ${p4err_ts_last}][last linecount: ${p4err_lc_last}]" @@ -185,6 +188,7 @@ monitor_uptime () { uptime=$(grep uptime "$tmp_info_data" | awk '{print $3}') [[ -z "$uptime" ]] && uptime="0:0:0" uptime=${uptime//:/ } + # shellcheck disable=SC2206 arr=($uptime) hours=${arr[0]} mins=${arr[1]} @@ -193,9 +197,11 @@ monitor_uptime () { # Ensure base 10 arithmetic used to avoid overflow errors uptime_secs=$(((10#$hours * 3600) + (10#$mins * 60) + 10#$secs)) rm -f "$tmpfname" - echo "# HELP p4_server_uptime P4D Server uptime (seconds)" >> "$tmpfname" - echo "# TYPE p4_server_uptime counter" >> "$tmpfname" - echo "p4_server_uptime{${serverid_label}${sdpinst_label}} $uptime_secs" >> "$tmpfname" + { + echo "# HELP p4_server_uptime P4D Server uptime (seconds)" + echo "# TYPE p4_server_uptime counter" + echo "p4_server_uptime{${serverid_label}${sdpinst_label}} $uptime_secs" + } >> "$tmpfname" chmod 644 "$tmpfname" mv "$tmpfname" "$fname" } @@ -215,8 +221,7 @@ monitor_license () { no_license=$(grep -c "Server license: none" "$tmp_info_data") # Update every 60 mins [[ ! -f "$tmp_license_data" || $(find "$tmp_license_data" -mmin +60) ]] || return - $p4 license -u 2>&1 > "$tmp_license_data" - [[ $? -ne 0 ]] && return + $p4 license -u "$tmp_license_data" 2>&1 || return userCount=0 userLimit=0 @@ -229,18 +234,18 @@ monitor_license () { licenseIP_label="" if [[ $no_license -ne 1 ]]; then - userCount=$(grep userCount $tmp_license_data | awk '{print $3}') - userLimit=$(grep userLimit $tmp_license_data | awk '{print $3}') - licenseExpires=$(grep licenseExpires $tmp_license_data | awk '{print $3}') - licenseTimeRemaining=$(grep licenseTimeRemaining $tmp_license_data | awk '{print $3}') - supportExpires=$(grep supportExpires $tmp_license_data | awk '{print $3}') + userCount=$(grep userCount "$tmp_license_data" | awk '{print $3}') + userLimit=$(grep userLimit "$tmp_license_data" | awk '{print $3}') + licenseExpires=$(grep licenseExpires "$tmp_license_data" | awk '{print $3}') + licenseTimeRemaining=$(grep licenseTimeRemaining "$tmp_license_data" | awk '{print $3}') + supportExpires=$(grep supportExpires "$tmp_license_data" | awk '{print $3}') licenseInfo=$(grep "Server license: " "$tmp_info_data" | sed -e "s/Server license: //" | sed -Ee "s/\(expires [^\)]+\)//" | sed -Ee "s/\(support [^\)]+\)//" ) - if [[ -z $licenseTimeRemaining && ! -z $supportExpires ]]; then + if [[ -z "$licenseTimeRemaining" && -n "$supportExpires" ]]; then dt=$(date +%s) - licenseTimeRemaining=$(($supportExpires - $dt)) + licenseTimeRemaining=$((supportExpires - dt)) fi # Trim trailing spaces - licenseInfo=$(echo $licenseInfo | sed -Ee 's/[ ]+$//') + licenseInfo=$(echo "$licenseInfo" | sed -Ee 's/[ ]+$//') licenseIP=$(grep "Server license-ip: " "$tmp_info_data" | sed -e "s/Server license-ip: //") fi @@ -248,37 +253,48 @@ monitor_license () { licenseIP_label=",IP=\"${licenseIP:-none}\"" rm -f "$tmpfname" - echo "# HELP p4_licensed_user_count P4D Licensed User count" >> "$tmpfname" - echo "# TYPE p4_licensed_user_count gauge" >> "$tmpfname" - echo "p4_licensed_user_count{${serverid_label}${sdpinst_label}} $userCount" >> "$tmpfname" - echo "# HELP p4_licensed_user_limit P4D Licensed User Limit" >> "$tmpfname" - echo "# TYPE p4_licensed_user_limit gauge" >> "$tmpfname" - echo "p4_licensed_user_limit{${serverid_label}${sdpinst_label}} $userLimit" >> "$tmpfname" - if [[ ! -z $licenseExpires ]]; then - echo "# HELP p4_license_expires P4D License expiry (epoch secs)" >> "$tmpfname" - echo "# TYPE p4_license_expires gauge" >> "$tmpfname" - echo "p4_license_expires{${serverid_label}${sdpinst_label}} $licenseExpires" >> "$tmpfname" + { + echo "# HELP p4_licensed_user_count P4D Licensed User count" + echo "# TYPE p4_licensed_user_count gauge" + echo "p4_licensed_user_count{${serverid_label}${sdpinst_label}} $userCount" + echo "# HELP p4_licensed_user_limit P4D Licensed User Limit" + echo "# TYPE p4_licensed_user_limit gauge" + echo "p4_licensed_user_limit{${serverid_label}${sdpinst_label}} $userLimit" + } >> "$tmpfname" + if [[ -n "$licenseExpires" ]]; then + { + echo "# HELP p4_license_expires P4D License expiry (epoch secs)" + echo "# TYPE p4_license_expires gauge" + echo "p4_license_expires{${serverid_label}${sdpinst_label}} $licenseExpires" + } >> "$tmpfname" fi - echo "# HELP p4_license_time_remaining P4D License time remaining (secs)" >> "$tmpfname" - echo "# TYPE p4_license_time_remaining gauge" >> "$tmpfname" - echo "p4_license_time_remaining{${serverid_label}${sdpinst_label}} $licenseTimeRemaining" >> "$tmpfname" - if [[ ! -z $supportExpires ]]; then - echo "# HELP p4_license_support_expires P4D License support expiry (epoch secs)" >> "$tmpfname" - echo "# TYPE p4_license_support_expires gauge" >> "$tmpfname" - echo "p4_license_support_expires{${serverid_label}${sdpinst_label}} $supportExpires" >> "$tmpfname" + { + echo "# HELP p4_license_time_remaining P4D License time remaining (secs)" + echo "# TYPE p4_license_time_remaining gauge" + echo "p4_license_time_remaining{${serverid_label}${sdpinst_label}} $licenseTimeRemaining" + } >> "$tmpfname" + if [[ -n "$supportExpires" ]]; then + { + echo "# HELP p4_license_support_expires P4D License support expiry (epoch secs)" + echo "# TYPE p4_license_support_expires gauge" + echo "p4_license_support_expires{${serverid_label}${sdpinst_label}} $supportExpires" + } >> "$tmpfname" fi - echo "# HELP p4_license_info P4D License info" >> "$tmpfname" - echo "# TYPE p4_license_info gauge" >> "$tmpfname" - echo "p4_license_info{${serverid_label}${sdpinst_label}${licenseInfo_label}} 1" >> "$tmpfname" - echo "# HELP p4_license_IP P4D Licensed IP" >> "$tmpfname" - echo "# TYPE p4_license_IP" >> "$tmpfname" - echo "p4_license_IP{${serverid_label}${sdpinst_label}${licenseIP_label}} 1" >> "$tmpfname" + { + echo "# HELP p4_license_info P4D License info" + echo "# TYPE p4_license_info gauge" + echo "p4_license_info{${serverid_label}${sdpinst_label}${licenseInfo_label}} 1" + echo "# HELP p4_license_IP P4D Licensed IP" + echo "# TYPE p4_license_IP" + echo "p4_license_IP{${serverid_label}${sdpinst_label}${licenseIP_label}} 1" + } >> "$tmpfname" chmod 644 "$tmpfname" mv "$tmpfname" "$fname" } monitor_filesys () { + local -i configurablesOK # Log current filesys.*.min settings # p4 configure show can give 2 values, or just the (default) # filesys.P4ROOT.min=5G (configure) @@ -291,20 +307,23 @@ monitor_filesys () { configurables="filesys.depot.min filesys.P4ROOT.min filesys.P4JOURNAL.min filesys.P4LOG.min filesys.TEMP.min" echo "" > "$tmp_filesys_data" + configurablesOK=1 for c in $configurables do - $p4 configure show "$c" >> "$tmp_filesys_data" + $p4 configure show "$c" >> "$tmp_filesys_data" || configurablesOK=0 done - [[ $? -ne 0 ]] && return + [[ "$configurablesOK" -eq 1 ]] || return rm -f "$tmpfname" - echo "# HELP p4_filesys_min Minimum space for filesystem" >> "$tmpfname" - echo "# TYPE p4_filesys_min gauge" >> "$tmpfname" + { + echo "# HELP p4_filesys_min Minimum space for filesystem" + echo "# TYPE p4_filesys_min gauge" + } >> "$tmpfname" for c in $configurables do - configuredValue=$(egrep "$c=.*configure" $tmp_filesys_data | awk '{print $1}' | awk -F= '{print $2}') - defaultValue=$(egrep "$c=.*default" $tmp_filesys_data | awk '{print $1}' | awk -F= '{print $2}') + configuredValue=$(grep -E "$c=.*configure" "$tmp_filesys_data" | awk '{print $1}' | awk -F= '{print $2}') + defaultValue=$(grep -E "$c=.*default" "$tmp_filesys_data" | awk '{print $1}' | awk -F= '{print $2}') value="$configuredValue" [[ -z "$configuredValue" ]] && value="$defaultValue" # Use ask to dehumanise 1G or 500m @@ -331,25 +350,29 @@ monitor_versions () { fname="$metrics_root/p4_version_info${sdpinst_suffix}-${SERVER_ID}.prom" tmpfname="$fname.$$" - p4dVersion=$(grep "Server version:" $tmp_info_data | sed -e 's/Server version: //' | sed -Ee 's/ \([0-9/]+\)//') + p4dVersion=$(grep "Server version:" "$tmp_info_data" | sed -e 's/Server version: //' | sed -Ee 's/ \([0-9/]+\)//') p4dVersion_label=",version=\"${p4dVersion:-unknown}\"" - p4dServices=$(grep "Server services:" $tmp_info_data | sed -e 's/Server services: //') + p4dServices=$(grep "Server services:" "$tmp_info_data" | sed -e 's/Server services: //') p4dServices_label=",services=\"${p4dServices:-unknown}\"" rm -f "$tmpfname" - echo "# HELP p4_p4d_build_info P4D Version/build info" >> "$tmpfname" - echo "# TYPE p4_p4d_build_info gauge" >> "$tmpfname" - echo "p4_p4d_build_info{${serverid_label}${sdpinst_label}${p4dVersion_label}} 1" >> "$tmpfname" - echo "# HELP p4_p4d_server_type P4D server type/services" >> "$tmpfname" - echo "# TYPE p4_p4d_server_type gauge" >> "$tmpfname" - echo "p4_p4d_server_type{${serverid_label}${sdpinst_label}${p4dServices_label}} 1" >> "$tmpfname" + { + echo "# HELP p4_p4d_build_info P4D Version/build info" + echo "# TYPE p4_p4d_build_info gauge" + echo "p4_p4d_build_info{${serverid_label}${sdpinst_label}${p4dVersion_label}} 1" + echo "# HELP p4_p4d_server_type P4D server type/services" + echo "# TYPE p4_p4d_server_type gauge" + echo "p4_p4d_server_type{${serverid_label}${sdpinst_label}${p4dServices_label}} 1" + } >> "$tmpfname" if [[ $UseSDP -eq 1 && -f "/p4/sdp/Version" ]]; then SDPVersion=$(cat "/p4/sdp/Version") SDPVersion_label=",version=\"${SDPVersion:-unknown}\"" - echo "# HELP p4_sdp_version SDP Version" >> "$tmpfname" - echo "# TYPE p4_sdp_version gauge" >> "$tmpfname" - echo "p4_sdp_version{${serverid_label}${sdpinst_label}${SDPVersion_label}} 1" >> "$tmpfname" + { + echo "# HELP p4_sdp_version SDP Version" + echo "# TYPE p4_sdp_version gauge" + echo "p4_sdp_version{${serverid_label}${sdpinst_label}${SDPVersion_label}} 1" + } >> "$tmpfname" fi chmod 644 "$tmpfname" @@ -361,7 +384,7 @@ monitor_ssl () { fname="$metrics_root/p4_ssl_info${sdpinst_suffix}-${SERVER_ID}.prom" tmpfname="$fname.$$" - certExpiry=$(grep "Server cert expires:" $tmp_info_data | sed -e 's/Server cert expires: //') + certExpiry=$(grep "Server cert expires:" "$tmp_info_data" | sed -e 's/Server cert expires: //') if [[ -z "$certExpiry" ]]; then return fi @@ -369,9 +392,11 @@ monitor_ssl () { certExpirySecs=$(date -d "$certExpiry" +%s) rm -f "$tmpfname" - echo "# HELP p4_ssl_cert_expires P4D SSL certificate expiry epoch seconds" >> "$tmpfname" - echo "# TYPE p4_ssl_cert_expires gauge" >> "$tmpfname" - echo "p4_ssl_cert_expires{${serverid_label}${sdpinst_label}} $certExpirySecs" >> "$tmpfname" + { + echo "# HELP p4_ssl_cert_expires P4D SSL certificate expiry epoch seconds" + echo "# TYPE p4_ssl_cert_expires gauge" + echo "p4_ssl_cert_expires{${serverid_label}${sdpinst_label}} $certExpirySecs" + } >> "$tmpfname" chmod 644 "$tmpfname" mv "$tmpfname" "$fname" @@ -381,12 +406,14 @@ monitor_change () { # Latest changelist counter as single counter value fname="$metrics_root/p4_change${sdpinst_suffix}-${SERVER_ID}.prom" tmpfname="$fname.$$" - curr_change=$($p4 counters 2>&1 | egrep '^change =' | awk '{print $3}') - if [[ ! -z "$curr_change" ]]; then + curr_change=$($p4 counters 2>&1 | grep -E '^change =' | awk '{print $3}') + if [[ -n "$curr_change" ]]; then rm -f "$tmpfname" - echo "# HELP p4_change_counter P4D change counter" >> "$tmpfname" - echo "# TYPE p4_change_counter counter" >> "$tmpfname" - echo "p4_change_counter{${serverid_label}${sdpinst_label}} $curr_change" >> "$tmpfname" + { + echo "# HELP p4_change_counter P4D change counter" + echo "# TYPE p4_change_counter counter" + echo "p4_change_counter{${serverid_label}${sdpinst_label}} $curr_change" + } >> "$tmpfname" chmod 644 "$tmpfname" mv "$tmpfname" "$fname" fi @@ -400,16 +427,18 @@ monitor_processes () { $p4 monitor show > "$monfile" 2> /dev/null rm -f "$tmpfname" - echo "# HELP p4_monitor_by_cmd P4 running processes" >> "$tmpfname" - echo "# TYPE p4_monitor_by_cmd counter" >> "$tmpfname" - awk '{print $5}' "$monfile" | sort | uniq -c | while read count cmd + { + echo "# HELP p4_monitor_by_cmd P4 running processes" + echo "# TYPE p4_monitor_by_cmd counter" + } >> "$tmpfname" + awk '{print $5}' "$monfile" | sort | uniq -c | while read -r count cmd do echo "p4_monitor_by_cmd{${serverid_label}${sdpinst_label},cmd=\"$cmd\"} $count" >> "$tmpfname" done echo "# HELP p4_monitor_by_user P4 running processes" >> "$tmpfname" echo "# TYPE p4_monitor_by_user counter" >> "$tmpfname" - awk '{print $3}' "$monfile" | sort | uniq -c | while read count user + awk '{print $3}' "$monfile" | sort | uniq -c | while read -r count user do echo "p4_monitor_by_user{${serverid_label}${sdpinst_label},user=\"$user\"} $count" >> "$tmpfname" done @@ -419,9 +448,11 @@ monitor_processes () { else proc="p4d" fi - echo "# HELP p4_process_count P4 ps running processes" >> "$tmpfname" - echo "# TYPE p4_process_count counter" >> "$tmpfname" - pcount=$(ps ax | grep "$proc " | grep -v "grep $proc" | wc -l) + { + echo "# HELP p4_process_count P4 ps running processes" + echo "# TYPE p4_process_count counter" + } >> "$tmpfname" + pcount=$(pgrep -u "$P4USER" "$proc" | wc -l) echo "p4_process_count{${serverid_label}${sdpinst_label}} $pcount" >> "$tmpfname" chmod 644 "$tmpfname" @@ -444,12 +475,12 @@ monitor_completed_cmds () { fi # Get the current timestamp and linecount - p4log_ts_curr=$(stat -c %Y $p4logfile) - p4log_lc_curr=$(wc -l $p4logfile | awk '{print $1}') + p4log_ts_curr=$(stat -c %Y "$p4logfile") + p4log_lc_curr=$(wc -l "$p4logfile" | awk '{print $1}') # Update the data file echo "Updating data file:" echo "[p4log][curr timestamp: ${p4log_ts_curr}][curr linecount: ${p4log_lc_curr}]" - echo "p4log $p4log_ts_curr $p4log_lc_curr" >> $tmpdatafile + echo "p4log $p4log_ts_curr $p4log_lc_curr" >> "$tmpdatafile" # If the logfile current timestamp is less then the last timestamp delete prom and return [[ $p4log_ts_curr -gt $p4log_ts_last ]] || { rm -f "$fname"; return ; } @@ -461,9 +492,11 @@ monitor_completed_cmds () { num_cmds=$(grep -c ' completed ' "$p4logfile") fi rm -f "$tmpfname" - echo "#HELP p4_completed_cmds Completed p4 commands" >> "$tmpfname" - echo "#TYPE p4_completed_cmds counter" >> "$tmpfname" - echo "p4_completed_cmds{${serverid_label}${sdpinst_label}} $num_cmds" >> "$tmpfname" + { + echo "#HELP p4_completed_cmds Completed p4 commands" + echo "#TYPE p4_completed_cmds counter" + echo "p4_completed_cmds{${serverid_label}${sdpinst_label}} $num_cmds" + } >> "$tmpfname" chmod 644 "$tmpfname" mv "$tmpfname" "$fname" } @@ -476,41 +509,45 @@ monitor_checkpoint () { # both write to checkpoint.log! # The strings searched for have been present in SDP logs for years now... - [[ $UseSDP -eq 0 ]] && return # Not valid if SDP not in use + [[ "$UseSDP" -eq 0 ]] && return # Not valid if SDP not in use fname="$metrics_root/p4_checkpoint${sdpinst_suffix}-${SERVER_ID}.prom" tmpfname="$fname.$$" rm -f "$tmpfname" - echo "#HELP p4_sdp_checkpoint_log_time Time of last checkpoint log" >> "$tmpfname" - echo "#TYPE p4_sdp_checkpoint_log_time gauge" >> "$tmpfname" + { + echo "#HELP p4_sdp_checkpoint_log_time Time of last checkpoint log" + echo "#TYPE p4_sdp_checkpoint_log_time gauge" + } >> "$tmpfname" # Look for latest checkpoint log which has Start/End (avoids run in progress and rotate_journal logs) ckp_log="" -# for f in $(ls -t /p4/$SDP_INSTANCE/logs/checkpoint.log*); - for f in $(find -L /p4/$SDP_INSTANCE/logs -type f -name checkpoint.log* -exec ls -t {} +) + # shellcheck disable=SC2044 + for f in $(find -L "/p4/$SDP_INSTANCE/logs" -type f -name "checkpoint.log*" -exec ls -t {} +) do - if [[ `grep -cE "Start p4_$SDP_INSTANCE Checkpoint|End p4_$SDP_INSTANCE Checkpoint" $f` -eq 2 ]]; then + if [[ $(grep -cE "Start p4_$SDP_INSTANCE Checkpoint|End p4_$SDP_INSTANCE Checkpoint" "$f") -eq 2 ]]; then ckp_log="$f" break fi; done ckp_time=0 - if [[ ! -z "$ckp_log" ]]; then + if [[ -n "$ckp_log" ]]; then ckp_time=$(date -r "$ckp_log" +"%s") fi - echo "p4_sdp_checkpoint_log_time{${serverid_label}${sdpinst_label}} $ckp_time" >> "$tmpfname" - echo "#HELP p4_sdp_checkpoint_duration Time taken for last checkpoint/restore action" >> "$tmpfname" - echo "#TYPE p4_sdp_checkpoint_duration gauge" >> "$tmpfname" + { + echo "p4_sdp_checkpoint_log_time{${serverid_label}${sdpinst_label}} $ckp_time" + echo "#HELP p4_sdp_checkpoint_duration Time taken for last checkpoint/restore action" + echo "#TYPE p4_sdp_checkpoint_duration gauge" + } >> "$tmpfname" ckp_duration=0 - if [[ ! -z "$ckp_log" && $ckp_time -gt 0 ]]; then + if [[ -n "$ckp_log" && "$ckp_time" -gt 0 ]]; then dt=$(grep "Start p4_$SDP_INSTANCE Checkpoint" "$ckp_log" | sed -e 's/\/p4.*//') start_time=$(date -d "$dt" +"%s") dt=$(grep "End p4_$SDP_INSTANCE Checkpoint" "$ckp_log" | sed -e 's/\/p4.*//') end_time=$(date -d "$dt" +"%s") - ckp_duration=$(($end_time - $start_time)) + ckp_duration=$((end_time - start_time)) fi echo "p4_sdp_checkpoint_duration{${serverid_label}${sdpinst_label}} $ckp_duration" >> "$tmpfname" @@ -525,22 +562,24 @@ monitor_replicas () { valid_servers="" # Read like this to set global variables in loop - while read svr_id type services + while read -r svr_id services do if [[ $services =~ standard|replica|commit-server|edge-server|forwarding-replica|build-server|standby|forwarding-standby ]]; then valid_servers="$valid_servers $svr_id" fi - done < <($p4 -F "%serverID% %type% %services%" servers) + done < <($p4 -F "%serverID% %services%" servers) declare -A svr_jnl declare -A svr_pos - while read svr_id jnl pos + while read -r svr_id jnl pos do svr_jnl[$svr_id]=$jnl svr_pos[$svr_id]=$pos done < <($p4 -F "%serverID% %appliedJnl% %appliedPos%" servers -J) - echo "#HELP p4_replica_curr_jnl Current journal for server" >> "$tmpfname" - echo "#TYPE p4_replica_curr_jnl counter" >> "$tmpfname" + { + echo "#HELP p4_replica_curr_jnl Current journal for server" + echo "#TYPE p4_replica_curr_jnl counter" + } >> "$tmpfname" for s in $valid_servers do curr_jnl=${svr_jnl[$s]:-0} @@ -548,8 +587,10 @@ monitor_replicas () { echo "p4_replica_curr_jnl{${serverid_label}${sdpinst_label},servername=\"$s\"} $curr_jnl" >> "$tmpfname" done - echo "#HELP p4_replica_curr_pos Current journal for server" >> "$tmpfname" - echo "#TYPE p4_replica_curr_pos counter" >> "$tmpfname" + { + echo "#HELP p4_replica_curr_pos Current journal for server" + echo "#TYPE p4_replica_curr_pos counter" + } >> "$tmpfname" for s in $valid_servers do curr_pos=${svr_pos[$s]:-0} @@ -584,11 +625,14 @@ monitor_errors () { indError=$((indID-1)) rm -f "$tmpfname" - echo "#HELP p4_error_count Server errors by id" >> "$tmpfname" - echo "#TYPE p4_error_count counter" >> "$tmpfname" - while read count ss_id error_id level + { + echo "#HELP p4_error_count Server errors by id" + echo "#TYPE p4_error_count counter" + } >> "$tmpfname" + + while read -r count ss_id error_id level do - if [[ ! -z ${ss_id:-} ]]; then + if [[ -n ${ss_id:-} ]]; then subsystem=${subsystems[$ss_id]} [[ -z "$subsystem" ]] && subsystem=$ss_id echo "p4_error_count{${serverid_label}${sdpinst_label},subsystem=\"$subsystem\",error_id=\"$error_id\",level=\"$level\"} $count" >> "$tmpfname" @@ -608,13 +652,19 @@ monitor_pull () { pullfile="/tmp/pull.out" $p4 pull -l > "$pullfile" 2> /dev/null rm -f "$tmpfname" - echo "# HELP p4_pull_errors P4 pull transfers failed count" >> "$tmpfname" - echo "# TYPE p4_pull_errors counter" >> "$tmpfname" + + { + echo "# HELP p4_pull_errors P4 pull transfers failed count" + echo "# TYPE p4_pull_errors counter" + } >> "$tmpfname" + count=$(grep -cEa "failed\.$" "$pullfile") - echo "p4_pull_errors{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + { + echo "p4_pull_errors{${serverid_label}${sdpinst_label}} $count" + echo "# HELP p4_pull_queue P4 pull files in queue count" + echo "# TYPE p4_pull_queue counter" + } >> "$tmpfname" - echo "# HELP p4_pull_queue P4 pull files in queue count" >> "$tmpfname" - echo "# TYPE p4_pull_queue counter" >> "$tmpfname" count=$(grep -cvEa "failed\.$" "$pullfile") echo "p4_pull_queue{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" @@ -625,8 +675,9 @@ monitor_pull () { monitor_realtime () { # p4d --show-realtime - only for 2021.1 or greater # Intially only available for SDP - [[ $UseSDP -eq 1 ]] || return - p4dver=$($P4DBIN -V |grep Rev.|awk -F / '{print $3}' ) + [[ "$UseSDP" -eq 1 ]] || return + p4dver=$("$P4DBIN" -V |grep Rev.|awk -F / '{print $3}' ) + # shellcheck disable=SC2072 [[ "$p4dver" > "2020.0" ]] || return realtimefile="/tmp/show-realtime.out" @@ -650,81 +701,97 @@ monitor_realtime () { origname="rtv.db.lockwait" mname="p4_${origname//./_}" count=$(grep "$origname" "$realtimefile" | awk '{print $4}') - if [[ ! -z $count ]]; then - metric_count=$(($metric_count + 1)) - echo "# HELP $mname P4 realtime lockwait counter" >> "$tmpfname" - echo "# TYPE $mname gauge" >> "$tmpfname" - echo "$mname{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + if [[ -n "$count" ]]; then + metric_count=$((metric_count + 1)) + { + echo "# HELP $mname P4 realtime lockwait counter" + echo "# TYPE $mname gauge" + echo "$mname{${serverid_label}${sdpinst_label}} $count" + } >> "$tmpfname" fi origname="rtv.db.ckp.active" mname="p4_${origname//./_}" count=$(grep "$origname" "$realtimefile" | awk '{print $4}') - if [[ ! -z $count ]]; then - metric_count=$(($metric_count + 1)) - echo "# HELP $mname P4 realtime checkpoint active indicator" >> "$tmpfname" - echo "# TYPE $mname gauge" >> "$tmpfname" - echo "$mname{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + if [[ -n "$count" ]]; then + metric_count=$((metric_count + 1)) + { + echo "# HELP $mname P4 realtime checkpoint active indicator" + echo "# TYPE $mname gauge" + echo "$mname{${serverid_label}${sdpinst_label}} $count" + } >> "$tmpfname" fi origname="rtv.db.ckp.records" mname="p4_${origname//./_}" count=$(grep "$origname" "$realtimefile" | awk '{print $4}') - if [[ ! -z $count ]]; then - metric_count=$(($metric_count + 1)) - echo "# HELP $mname P4 realtime checkpoint records counter" >> "$tmpfname" - echo "# TYPE $mname gauge" >> "$tmpfname" - echo "$mname{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + if [[ -n "$count" ]]; then + metric_count=$((metric_count + 1)) + { + echo "# HELP $mname P4 realtime checkpoint records counter" + echo "# TYPE $mname gauge" + echo "$mname{${serverid_label}${sdpinst_label}} $count" + } >> "$tmpfname" fi origname="rtv.db.io.records" mname="p4_${origname//./_}" count=$(grep "$origname" "$realtimefile" | awk '{print $4}') - if [[ ! -z $count ]]; then - metric_count=$(($metric_count + 1)) - echo "# HELP $mname P4 realtime IO records counter" >> "$tmpfname" - echo "# TYPE $mname counter" >> "$tmpfname" - echo "$mname{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + if [[ -n "$count" ]]; then + metric_count=$((metric_count + 1)) + { + echo "# HELP $mname P4 realtime IO records counter" + echo "# TYPE $mname counter" + echo "$mname{${serverid_label}${sdpinst_label}} $count" + } >> "$tmpfname" fi origname="rtv.rpl.behind.bytes" mname="p4_${origname//./_}" count=$(grep "$origname" "$realtimefile" | awk '{print $4}') - if [[ ! -z $count ]]; then - metric_count=$(($metric_count + 1)) - echo "# HELP $mname P4 realtime replica bytes lag counter" >> "$tmpfname" - echo "# TYPE $mname gauge" >> "$tmpfname" - echo "$mname{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + if [[ -n "$count" ]]; then + metric_count=$((metric_count + 1)) + { + echo "# HELP $mname P4 realtime replica bytes lag counter" + echo "# TYPE $mname gauge" + echo "$mname{${serverid_label}${sdpinst_label}} $count" + } >> "$tmpfname" fi origname="rtv.rpl.behind.journals" mname="p4_${origname//./_}" count=$(grep "$origname" "$realtimefile" | awk '{print $4}') - if [[ ! -z $count ]]; then - metric_count=$(($metric_count + 1)) - echo "# HELP $mname P4 realtime replica journal lag counter" >> "$tmpfname" - echo "# TYPE $mname gauge" >> "$tmpfname" - echo "$mname{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + if [[ -n "$count" ]]; then + metric_count=$((metric_count + 1)) + { + echo "# HELP $mname P4 realtime replica journal lag counter" + echo "# TYPE $mname gauge" + echo "$mname{${serverid_label}${sdpinst_label}} $count" + } >> "$tmpfname" fi origname="rtv.svr.sessions.active" mname="p4_${origname//./_}" count=$(grep "$origname" "$realtimefile" | awk '{print $4}') - if [[ ! -z $count ]]; then - metric_count=$(($metric_count + 1)) - echo "# HELP $mname P4 realtime server active sessions counter" >> "$tmpfname" - echo "# TYPE $mname gauge" >> "$tmpfname" - echo "$mname{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + if [[ -n $count ]]; then + metric_count=$((metric_count + 1)) + { + echo "# HELP $mname P4 realtime server active sessions counter" + echo "# TYPE $mname gauge" + echo "$mname{${serverid_label}${sdpinst_label}} $count" + } >> "$tmpfname" fi origname="rtv.svr.sessions.total" mname="p4_${origname//./_}" count=$(grep "$origname" "$realtimefile" | awk '{print $4}') - if [[ ! -z $count ]]; then - metric_count=$(($metric_count + 1)) - echo "# HELP $mname P4 realtime server total sessions counter" >> "$tmpfname" - echo "# TYPE $mname counter" >> "$tmpfname" - echo "$mname{${serverid_label}${sdpinst_label}} $count" >> "$tmpfname" + if [[ -n "$count" ]]; then + metric_count=$((metric_count + 1)) + { + echo "# HELP $mname P4 realtime server total sessions counter" + echo "# TYPE $mname counter" + echo "$mname{${serverid_label}${sdpinst_label}} $count" + } >> "$tmpfname" fi if [[ $metric_count -gt 0 ]]; then @@ -735,9 +802,9 @@ monitor_realtime () { update_data_file () { echo "Updating data file:" - cat $tmpdatafile - rm -f $data_file - mv $tmpdatafile $data_file + cat "$tmpdatafile" + rm -f "$data_file" + mv "$tmpdatafile" "$data_file" } load_data_file @@ -757,4 +824,4 @@ monitor_ssl update_data_file # Make sure all readable by node_exporter or other user -chmod 644 $metrics_root/*.prom +chmod 644 "$metrics_root"/*.prom From 83c5d19a1b4e6d96d016a270c2b0fb057921c37a Mon Sep 17 00:00:00 2001 From: "C. Thomas Tyler" Date: Sat, 6 Aug 2022 23:30:43 -0400 Subject: [PATCH 3/9] ShellCheck for push_metrics.sh, and standardized usage() calls & behavior for {push,monitor}_metrics.sh. --- scripts/monitor_metrics.sh | 8 +++++--- scripts/push_metrics.sh | 16 +++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/scripts/monitor_metrics.sh b/scripts/monitor_metrics.sh index d83d258..419fb2d 100755 --- a/scripts/monitor_metrics.sh +++ b/scripts/monitor_metrics.sh @@ -60,6 +60,8 @@ monitor_metrics.sh -h # Add full manual page documentation here. true fi + + exit 2 } : Command Line Processing @@ -70,14 +72,14 @@ declare -i UseSDP=1 set +u while [[ $# -gt 0 ]]; do case $1 in - (-h) usage -h && exit 0;; - # (-man) usage -man;; + (-h) usage -h;; + (-man) usage -man;; (-p) Port=$2; shiftArgs=1;; (-u) User=$2; shiftArgs=1;; (-m) metrics_root=$2; shiftArgs=1;; (-d) data_file=$2; shiftArgs=1;; (-nosdp) UseSDP=0;; - (-*) usage -h "Unknown command line option ($1)." && exit 1;; + (-*) usage -h "Unknown command line option ($1).";; (*) export SDP_INSTANCE=$1;; esac diff --git a/scripts/push_metrics.sh b/scripts/push_metrics.sh index b43f919..d88e92f 100755 --- a/scripts/push_metrics.sh +++ b/scripts/push_metrics.sh @@ -29,7 +29,7 @@ metrics_logfile="/p4/1/logs/push_metrics.log" function msg () { echo -e "$*"; } function log () { dt=$(date '+%Y-%m-%d %H:%M:%S'); echo -e "$dt: $*" >> "$metrics_logfile"; msg "$dt: $*"; } -function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; } +function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; } function usage { @@ -52,6 +52,12 @@ Takes node_exporter metrics and pushes them to pushgateway instance centrally. This is not normally required on customer machines. It assumes an SDP setup. " + + if [[ "$style" == -man ]]; then + # Add full manual page documentation here. + true + fi + exit 2 } # Command Line Processing @@ -62,10 +68,10 @@ ConfigFile=/p4/common/config/.push_metrics.cfg set +u while [[ $# -gt 0 ]]; do case $1 in - (-h) usage -h && exit 0;; - # (-man) usage -man;; + (-h) usage -h;; + (-man) usage -man;; (-c) ConfigFile=$2; shiftArgs=1;; - (-*) usage -h "Unknown command line option ($1)." && exit 1;; + (-*) usage -h "Unknown command line option ($1).";; esac # Shift (modify $#) the appropriate number of times. @@ -110,7 +116,7 @@ max_iterations=10 STATUS=1 while [ $STATUS -ne 0 ]; do sleep 1 - ((iterations=$iterations+1)) + iterations=$((iterations+1)) log "Pushing metrics" result=$(curl --retry 5 --user "$metrics_user:$metrics_passwd" --data-binary @_push.log "$metrics_host/metrics/job/$metrics_job/instance/$metrics_instance/customer/$metrics_customer") STATUS=0 From 93ffb206c3b4d7c32950db43959061ceb7dac2db Mon Sep 17 00:00:00 2001 From: "C. Thomas Tyler" Date: Sun, 7 Aug 2022 09:27:52 -0400 Subject: [PATCH 4/9] ShellCheck for check_for_updates.sh; standardized usage() calls, fixed usage bug, improved error message. --- scripts/check_for_updates.sh | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/scripts/check_for_updates.sh b/scripts/check_for_updates.sh index 6e64f7d..239d5e0 100755 --- a/scripts/check_for_updates.sh +++ b/scripts/check_for_updates.sh @@ -11,7 +11,7 @@ github_url="https://api.github.com/repos/perforce/p4prometheus/commits?per_page= github_download_url="https://raw.githubusercontent.com/perforce/p4prometheus/master/scripts" function msg () { echo -e "$*"; } -function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; } +function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; } function usage { @@ -35,6 +35,12 @@ Uses the github API and stores a local file with current status. Depends on 'curl' and 'jq' being in the path. " + if [[ "$style" == -man ]]; then + # Add full manual page documentation here. + true + fi + + exit 2 } SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" @@ -49,10 +55,10 @@ ConfigFile=".update_config" set +u while [[ $# -gt 0 ]]; do case $1 in - (-h) usage -h && exit 0;; - # (-man) usage -man;; + (-h) usage -h;; + (-man) usage -man;; (-c) ConfigFile=$2; shiftArgs=1;; - (-*) usage -h "Unknown command line option ($1)." && exit 1;; + (-*) usage -h "Unknown command line option ($1).";; esac # Shift (modify $#) the appropriate number of times. @@ -67,18 +73,15 @@ set -u cd "$SCRIPT_DIR" || bail "Can't cd to $SCRIPT_DIR" # Check for dependencies - -curl=$(which curl) -[[ $? -eq 0 ]] || bail "Failed to find curl in path" -jq=$(which jq) -[[ $? -eq 0 ]] || bail "Failed to find jq in path" +[[ -n "$(command -v curl)" ]] || bail "Failed to find curl in PATH." +[[ -n "$(command -v jq)" ]] || bail "Failed to find jq in PATH." last_github_sha="" -last_github_date="" +#last_github_date="" if [[ -e "$ConfigFile" ]]; then last_github_sha=$(grep last_github_sha "$ConfigFile" | cut -d= -f2) - last_github_date=$(grep last_github_date "$ConfigFile" | cut -d= -f2) + #last_github_date=$(grep last_github_date "$ConfigFile" | cut -d= -f2) fi github_sha=$(curl "$github_url" | jq '.[] | .sha') From 07de5847f0ea498465b84e24baf6f48bc204c52a Mon Sep 17 00:00:00 2001 From: "C. Thomas Tyler" Date: Sun, 7 Aug 2022 09:43:33 -0400 Subject: [PATCH 5/9] ShellCheck for install_{p4prom,prom_graf}.sh, and standardized usage() calls, fixed bug. --- scripts/install_p4prom.sh | 17 ++++++++++++----- scripts/install_prom_graf.sh | 13 ++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/scripts/install_p4prom.sh b/scripts/install_p4prom.sh index 83d0f63..6ed4882 100755 --- a/scripts/install_p4prom.sh +++ b/scripts/install_p4prom.sh @@ -27,6 +27,7 @@ function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; } function usage { + declare style=${1:--h} declare errorMessage=${2:-Unset} if [[ "$errorMessage" != Unset ]]; then @@ -58,6 +59,12 @@ Examples: ./install_p4prom.sh -nosdp -m /p4metrics -u perforce " + if [[ "$style" == -man ]]; then + # Add full manual page documentation here. + true + fi + + exit 2 } # Command Line Processing @@ -72,14 +79,14 @@ declare P4LOG="" set +u while [[ $# -gt 0 ]]; do case $1 in - (-h) usage -h && exit 1;; - # (-man) usage -man;; + (-h) usage -h;; + (-man) usage -man;; (-m) metrics_root=$2; shiftArgs=1;; (-u) OsUser="$2"; shiftArgs=1;; (-push) InstallPushgateway=1;; (-nosdp) UseSDP=0;; (-l) P4LOG="$2"; shiftArgs=1;; - (-*) usage -h "Unknown command line option ($1)." && exit 1;; + (-*) usage -h "Unknown command line option ($1).";; (*) export SDP_INSTANCE=$1;; esac @@ -97,8 +104,7 @@ if [[ $(id -u) -ne 0 ]]; then exit 1 fi -wget=$(which wget) -[[ $? -eq 0 ]] || bail "Failed to find wget in path" +[[ -n "$(command -v wget)" ]] || bail "Failed to find wget in PATH." if command -v getenforce > /dev/null; then selinux=$(getenforce) @@ -126,6 +132,7 @@ if [[ $UseSDP -eq 1 ]]; then source /p4/common/bin/p4_vars "$SDP_INSTANCE" ||\ { echo -e "\\nError: Failed to load SDP environment.\\n"; exit 1; } + # shellcheck disable=SC2153 p4="$P4BIN -u $P4USER -p $P4PORT" $p4 info -s || bail "Can't connect to P4PORT: $P4PORT" p4prom_config_dir="/p4/common/config" diff --git a/scripts/install_prom_graf.sh b/scripts/install_prom_graf.sh index f4c24c9..cfbf4bf 100755 --- a/scripts/install_prom_graf.sh +++ b/scripts/install_prom_graf.sh @@ -24,6 +24,7 @@ function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; } function usage { + declare style=${1:--h} declare errorMessage=${2:-Unset} if [[ "$errorMessage" != Unset ]]; then @@ -41,6 +42,12 @@ or -push Means install pushgateway (otherwise it won't be installed) " + if [[ "$style" == -man ]]; then + # Add full manual page documentation here. + true + fi + + exit 2 } # Command Line Processing @@ -51,10 +58,10 @@ declare -i InstallPushgateway=0 set +u while [[ $# -gt 0 ]]; do case $1 in - (-h) usage -h && exit 1;; + (-h) usage -h;; (-push) InstallPushgateway=1;; - # (-man) usage -man;; - (-*) usage -h "Unknown command line option ($1)." && exit 1;; + (-man) usage -man;; + (-*) usage -h "Unknown command line option ($1).";; esac # Shift (modify $#) the appropriate number of times. From 9a0a8a983c495de07ee1a30d88deac81ce4b6d11 Mon Sep 17 00:00:00 2001 From: "C. Thomas Tyler" Date: Sun, 7 Aug 2022 09:48:26 -0400 Subject: [PATCH 6/9] ShellCheck for monitor_metrics_wrapper.sh, and standardized usage() calls, fixed bug. --- scripts/monitor_wrapper.sh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/scripts/monitor_wrapper.sh b/scripts/monitor_wrapper.sh index 895d46a..3a324c0 100755 --- a/scripts/monitor_wrapper.sh +++ b/scripts/monitor_wrapper.sh @@ -24,7 +24,7 @@ metrics_root=/p4/metrics function msg () { echo -e "$*"; } -function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; } +function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; } function usage { @@ -43,6 +43,12 @@ monitor_wrapper.sh [ | -nosdp] [-p ] | [-u ] | [-m Date: Sun, 7 Aug 2022 10:12:00 -0400 Subject: [PATCH 7/9] ShellCheck for update_p4prom.sh, and standardized usage() calls, fixed bug. --- scripts/update_p4prom.sh | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/scripts/update_p4prom.sh b/scripts/update_p4prom.sh index ac8856b..46d9db6 100755 --- a/scripts/update_p4prom.sh +++ b/scripts/update_p4prom.sh @@ -27,6 +27,7 @@ function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; } function usage { + declare style=${1:--h} declare errorMessage=${2:-Unset} if [[ "$errorMessage" != Unset ]]; then @@ -57,6 +58,12 @@ Examples: ./update_p4prom.sh -nosdp -m /p4metrics -u perforce " + if [[ "$style" == -man ]]; then + # Add full manual page documentation here. + true + fi + + exit 2 } # Command Line Processing @@ -65,18 +72,18 @@ declare -i shiftArgs=0 declare -i UseSDP=1 declare -i SELinuxEnabled=0 declare OsUser="" -declare P4LOG="" +#declare P4LOG="" set +u while [[ $# -gt 0 ]]; do case $1 in - (-h) usage -h && exit 1;; - # (-man) usage -man;; + (-h) usage -h;; + (-man) usage -man;; (-m) metrics_root=$2; shiftArgs=1;; (-u) OsUser="$2"; shiftArgs=1;; (-nosdp) UseSDP=0;; - (-l) P4LOG="$2"; shiftArgs=1;; - (-*) usage -h "Unknown command line option ($1)." && exit 1;; + #(-l) P4LOG="$2"; shiftArgs=1;; + (-*) usage -h "Unknown command line option ($1).";; (*) export SDP_INSTANCE=$1;; esac @@ -94,8 +101,7 @@ if [[ $(id -u) -ne 0 ]]; then exit 1 fi -wget=$(which wget) -[[ $? -eq 0 ]] || bail "Failed to find wget in path" +[[ -n "$(command -v wget)" ]] || bail "Failed to find wget in PATH." if command -v getenforce > /dev/null; then selinux=$(getenforce) @@ -123,10 +129,11 @@ if [[ $UseSDP -eq 1 ]]; then source /p4/common/bin/p4_vars "$SDP_INSTANCE" ||\ { echo -e "\\nError: Failed to load SDP environment.\\n"; exit 1; } + # shellcheck disable=SC2153 p4="$P4BIN -u $P4USER -p $P4PORT" $p4 info -s || bail "Can't connect to P4PORT: $P4PORT" p4prom_config_dir="/p4/common/config" - p4prom_bin_dir="/p4/common/site/bin" + ###p4prom_bin_dir="/p4/common/site/bin" else SDP_INSTANCE="" p4port=${Port:-$P4PORT} @@ -136,7 +143,7 @@ else p4="p4 -u $p4user -p $p4port" $p4 info -s || bail "Can't connect to P4PORT: $p4port" p4prom_config_dir="/etc/p4prometheus" - p4prom_bin_dir="$p4prom_config_dir" + ###p4prom_bin_dir="$p4prom_config_dir" fi p4prom_config_file="$p4prom_config_dir/p4prometheus.yaml" From 2d2a2aee4440c9f385e2aed7ebbf23d4a33ab323 Mon Sep 17 00:00:00 2001 From: "C. Thomas Tyler" Date: Sun, 7 Aug 2022 10:14:53 -0400 Subject: [PATCH 8/9] ShellCheck for upload_grafana_dashboard.sh. --- scripts/upload_grafana_dashboard.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/upload_grafana_dashboard.sh b/scripts/upload_grafana_dashboard.sh index 3e47fc9..7740d4f 100755 --- a/scripts/upload_grafana_dashboard.sh +++ b/scripts/upload_grafana_dashboard.sh @@ -24,7 +24,7 @@ HELPINFO } function msg () { echo -e "$*"; } -function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; } +function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; } # ------------------------------------------------------------------------- if [ -z "$1" ];then @@ -51,5 +51,5 @@ DASHBOARD=$1 curl -X POST \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer $GRAFANA_API_KEY" \ - -d @${DASHBOARD} \ + -d "@${DASHBOARD}" \ "$GRAFANA_SERVER/api/dashboards/db" -w "\n" | tee -a "$logfile" From 0bdf04132fa2a3b38ecc9f8fd8b93d0239d73745 Mon Sep 17 00:00:00 2001 From: "C. Thomas Tyler" Date: Thu, 1 Sep 2022 10:42:50 -0400 Subject: [PATCH 9/9] Fixed issue with missing redirect operator. --- scripts/monitor_metrics.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/monitor_metrics.sh b/scripts/monitor_metrics.sh index 419fb2d..5fb1445 100755 --- a/scripts/monitor_metrics.sh +++ b/scripts/monitor_metrics.sh @@ -223,7 +223,7 @@ monitor_license () { no_license=$(grep -c "Server license: none" "$tmp_info_data") # Update every 60 mins [[ ! -f "$tmp_license_data" || $(find "$tmp_license_data" -mmin +60) ]] || return - $p4 license -u "$tmp_license_data" 2>&1 || return + $p4 license -u > "$tmp_license_data" 2>&1 || return userCount=0 userLimit=0