Skip to content

Instantly share code, notes, and snippets.

@Himan10
Created October 18, 2020 15:21
Show Gist options
  • Select an option

  • Save Himan10/ca7c7fb1b10f2a04d55161e1489b59ef to your computer and use it in GitHub Desktop.

Select an option

Save Himan10/ca7c7fb1b10f2a04d55161e1489b59ef to your computer and use it in GitHub Desktop.
check stats of RAM and SWAP, and notify once any one of them exceeds the limit.
#! /bin/bash
# notify the user
notify-send -u low -t 8000 "checkRAWSWAP" "Monitoring Starts"
### getthe memory and swap int.
var=0
while true; do
mem=$(free -m | grep -P 'Mem.*' --line-buffered | sed -r 's/(\s)+/_/g' | cut -d '_' -f 3)
# g flag in sed command means "globally", change should affect other matched parts too.
if [ $mem -gt 2100 ] && [ $var -eq 0 ]
then
python /home/hi-man/python/pyproject/notifywhenLOAD/notifyME.py "RAM" $mem "show"
var=1
elif [ $mem -gt 2350 ] && [ $var -eq 1 ]
then
python /home/hi-man/python/pyproject/notifywhenLOAD/notifyME.py "RAM" $mem "update"
i=0
while [ $i -lt 10 ] && [ $mem -gt 2350 ]; do
if [ $i -eq 9 ]
then
pidCommand=$(ps -u hi-man -o pid,%mem,command | sort -bhr -k 2 | head -n 1 | awk '{print $1 $3}')
# seperate the pid and command
pid=$(echo "$pidCommand" | grep -Po '^\d+')
commandName=$(echo "$pidCommand" | grep -Po '.(?<=/)[\w\d\S]+')
#kill -STOP $pid
notify-send -t 6000 -u normal "MOST CONSUMING $pid" "$commandName"
fi
i=$((i+1))
done
# send the pause signal to application after N seconds.
fi
sleep 0.3 # Pause the current (MEM) and check for others (Swap)
### Check for SWAP
swap=$(free -m | grep -P 'Swap.*' --line-buffered | sed -r 's/(\s)+/_/g' | cut -d '_' -f 3)
if [ $swap -gt 20 ]
then
python /home/hi-man/python/pyproject/notifywhenLOAD/notifyME.py "SWAP" $swap "update"
i=0
while [ $i -lt 10 ] && [ $swap -gt 15 ]; do
sleep 0.5
if [ $i -eq 5 ]
then
notify-send -u normal -t 1500 'Executing After 5 sec' 'sudo swapoff -a'
elif [ $i -eq 10 ]
then
# Anyone can open the file and read the password here, let's save myself
echo -e '{mypassword}\n' | sudo -S swapoff -a >> /dev/null
fi
i=$((i+1))
done
fi
if [ $mem -lt 2000 ] && [ $swap -eq 0 ] && [ $var -eq 1 ]
then
python /home/hi-man/python/pyproject/notifywhenLOAD/notifyME.py "NORMAL" 0 "show"
#kill -CONT $pid
var=0
fi
done
#!/bin/python
import sys
import notify2
def notification(message: str):
"""
Display notification to the desktop
Task:
1. show() -> it will generate a complete new pop
2. update() -> it will update the payload part of same notification pop-up, not issuing any new one.
Usage : python <filename.py> typeObj:str value:int objective:str
typeObj: RAM/SWAP/NORMAL
value: current usage of RAM or SWAP (for NORMAL, the value = 0)
objective: show/update
"""
# initialize the notification
notify2.init('notifywhenLOAD')
notifyObj = notify2.Notification('Emergency Alert!', message)
notifyObj.set_timeout(12000)
return notifyObj
def main():
a = notification(f'{sys.argv[1]} exceeds {sys.argv[2]}')
if sys.argv[1] in ['RAM', 'SWAP'] and sys.argv[3] == 'update':
a.update(f'{sys.argv[1]} Alert!! Warning for death')
#a.update('river')
a.set_urgency(2)
a.show()
elif sys.argv[1] in ['RAM', 'SWAP'] and sys.argv[3] == 'show':
a.set_timeout(10000)
a.set_urgency(1)
a.show()
elif sys.argv[1] == 'NORMAL':
a.update('ChiLLax!!! Nothing to worry about')
a.set_urgency(0)
a.show()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment