diff --git a/psi-alerts.sh b/psi-alerts.sh index 9f04c61..fc6bc0b 100755 --- a/psi-alerts.sh +++ b/psi-alerts.sh @@ -261,55 +261,51 @@ check_dunst_id_is_visible () { } local current_alarm="" -local last_alarm="" typeset -A notice_sent typeset -A secs integer last_dunst_id=-1 local last_line="" -set -x +#set -x while true; do local line=$(journalctl -u ${svc} -n1) + local now=$(date +%s) + local last_timestamp=$(date -d "$(awk '{print $1" "$2" "$3}' <<< "${line}")" +%s) + local time_diff=$(( now - last_timestamp )) if [[ "${last_line}" == "${line}" ]]; then - # line hasn't changed since last run, do nothing + # last line hasn't changed, check to see if we can clear alarms + if (( time_diff >= 3 )); then + # haven't seen a monitor alert for 3 seconds, see if we can clear them + if [[ -n "${current_alarms}" ]]; then + typeset -a alarms=( $(tr '|' ' ' <<< "$current_alarms") ) + for alarm in ${alarms}; do + integer elapsed=$(( now - ${secs[${alarm}]} )) + if is_clear "${alarm}" && (( elapsed >= 300 )); then + current_alarms=$(sed -E "s/${alarm}\|?//" <<< "${current_alarms}") + last_alarm=$(awk -F'|' '{print $NF}' <<< "${current_alarms}") + unset "notice_sent[${alarm}]" + unset "secs[${alarm}]" + fi + done + fi + sleep 1 + continue + fi + sleep 1 continue fi last_line="${line}" - local now=$(date +%s) - local last_timestamp=$(date -d $(awk '{print $1" "$2" "$3}' <<< "${line}") +%s) - local time_diff=$(( now - last_timestamp )) - if (( time_diff >= 3 )); then - # haven't seen a monitor alert for 3 seconds, see if we can clear them - if [[ -n "${current_alarms}" ]]; then - typeset -a alarms=( $(tr '|' ' ' <<< "$current_alarms") ) - for alarm in ${alarms}; do - integer elapsed=$(( now - ${secs[${alarm}]} )) - if is_clear "${alarm}" && (( elapsed >= 300 )); then - current_alarms=$(sed -E "s/${alarm}\|?//" <<< "${current_alarms}") - last_alarm=$(awk -F'|' '{print $NF}' <<< "${current_alarms}") - unset "notice_sent[${alarm}]" - unset "secs[${alarm}]" - fi - done - fi - sleep 1 - continue - fi local psi_type="$(grep -Eo "(CPU|MEM|IO) PSI event" <<< "${line}" | grep -Eo "CPU|MEM|IO")" if [[ -n "${psi_type}" ]]; then secs+=(${psi_type} ${now}) - if [[ "${psi_type}" != "${last_alarm}" ]]; then - if [[ ! ${notice_sent[${psi_type}]} ]]; then - last_dunst_id=$(exec_notices "${psi_type}" "${current_alarms}") - notice_sent+=(${psi_type} true) - elif (( last_dunst_id >= 0 )) && check_dunst_id_is_visible "${last_dunst_id}"; then - last_alarm="${psi_type}" - sleep 1 - continue - fi + if [[ ! ${notice_sent[${psi_type}]} ]]; then + last_dunst_id=$(exec_notices "${psi_type}" "${current_alarms}") + notice_sent+=(${psi_type} true) + elif (( last_dunst_id >= 0 )) && check_dunst_id_is_visible "${last_dunst_id}"; then + sleep 1 + continue fi - last_alarm="${psi_type}" if [[ -z "${current_alarms}" ]]; then current_alarms="${psi_type}" else @@ -320,5 +316,5 @@ while true; do fi sleep 1 done -set +x +#set +x