Skip to content

Commit a5e4084

Browse files
committed
Check Memory
0 parents  commit a5e4084

File tree

7 files changed

+140
-0
lines changed

7 files changed

+140
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
email.env

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Send Email utility
2+
This is a simple script for sending emails to an address list through the terminal. It requires a mailing list with target email addresses.
3+
It also requires a `email.env` file with configuration variables. See `email.env.default` and configure values accordingly.
4+
5+
## Usage
6+
Verify where python3 folder in server.
7+
```bash
8+
which python3
9+
```
10+
Set this path in first line on send-email file
11+
12+
```
13+
#!/usr/bin/python3
14+
```
15+
You may want to run `pip install python-dotenv` before using it.
16+
Set email.env with our gmail credentials.
17+
Add address in addr_list file to receive emails.
18+
19+
```bash
20+
chmod +x alertmemory.sh
21+
```
22+
23+
Run comand
24+
25+
```bash
26+
sudo ./alertmemory.sh
27+
```

addr_list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

alertmemory.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
#######################################################################################
3+
#Script Name :alertmemory.sh
4+
#Description :send alert mail when server memory is running low
5+
#Args :
6+
#Author :Aaron Kili Kisinga and Hand Medeiros an Guilherme Pontess
7+
8+
#License : GNU GPL-3
9+
#######################################################################################
10+
## declare mail variables
11+
## sudo apt-get install ssmtp
12+
## email subject
13+
## https://github.com/gpontesss/terminal-send-email
14+
## https://www.tecmint.com/shell-script-to-send-email-alert-when-memory-low/
15+
16+
subject="Server Memory Status Alert"
17+
18+
## get total free memory size in megabytes(MB)
19+
free=$(free -mt | grep Mem | awk '{print $4}')
20+
memory=$(free -mt | grep Mem | awk '{print $2}')
21+
22+
total_free=$((($free * 100)/ $memory))
23+
## If free memory is less or equals to 15%
24+
if [[ "$total_free" -le 15 ]]; then
25+
# Send email if system memory is running low
26+
if [ -e last_moment_sended_email ]; then
27+
# Date 1 : last moment sended email
28+
dt1=$(<last_moment_sended_email)
29+
t1=`date --date="$dt1" +%s`
30+
# Date 2 : Current date
31+
dt2=`date +%Y-%m-%d\ %H:%M:%S`
32+
t2=`date --date="$dt2" +%s`
33+
34+
# Compute the difference in dates in seconds
35+
let "tDiff=$t2-$t1"
36+
# Compute the approximate hour difference
37+
let "hDiff=$tDiff/60"
38+
else
39+
hDiff=61
40+
fi
41+
42+
if [[ "$hDiff" -gt 60 ]]; then
43+
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head | ./send-email -ml addr_list -t "$subject"
44+
#ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head | mail -s "$subject" [email protected]
45+
rm last_moment_sended_email
46+
echo `date +%Y-%m-%d\ %H:%M:%S` >> last_moment_sended_email
47+
fi
48+
fi
49+
50+
exit 0
51+
52+
# SSMTP
53+
# https://wiki.debian.org/sSMTP
54+
# http://www.devin.com.br/mail-via-linha-de-comando/
55+
# https://tecadmin.net/sendmail-to-relay-emails-through-gmail-stmp/
56+
# https://stackoverflow.com/questions/10359437/sendmail-how-to-configure-sendmail-on-ubuntu

email.env.default

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SMTP_EMAIL=
2+
SMTP_PASSWORD=
3+
SMTP_HOST=smtp.gmail.com
4+
SMTP_PORT=465

last_moment_sended_email

Whitespace-only changes.

send-email

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/python3
2+
3+
# Google reference for SMTP setup: https://support.google.com/mail/answer/7126229?visit_id=636933472854975930-1892577040&hl=pt-BR&rd=1
4+
5+
import os, sys, json, argparse
6+
from dotenv import load_dotenv
7+
from smtplib import SMTP_SSL
8+
from email.message import EmailMessage
9+
10+
def list_or_json(filename):
11+
addr_list = []
12+
with open(filename, "r") as addr_file:
13+
file_content = addr_file.read()
14+
try:
15+
addr_list = json.loads(file_content)
16+
except json.JSONDecodeError:
17+
addr_list = file_content.split('\n')
18+
return addr_list
19+
20+
# Parsing arguments
21+
parser = argparse.ArgumentParser(description="Send emails via cli.")
22+
parser.add_argument('-b', "--body", type=argparse.FileType('r'), default=sys.stdin, metavar="body_message",
23+
help="Name of file containing body message. It can also be passed through pipes to stdin.")
24+
parser.add_argument('-t', "--title", type=str, default="Python script email :)", metavar="email_title",
25+
help="Title of the email.")
26+
parser.add_argument('-ml', "--mailing-list", type=list_or_json, dest="addr_list", metavar="addresses_filename",
27+
help="Name of file containing mailing list. Can be a JSON list file.", required=True)
28+
29+
args = parser.parse_args(sys.argv[1:])
30+
31+
# Setting up environment variables
32+
load_dotenv('./email.env')
33+
34+
from_addr = os.getenv("SMTP_EMAIL")
35+
password = os.getenv("SMTP_PASSWORD")
36+
host = os.getenv("SMTP_HOST")
37+
port = os.getenv("SMTP_PORT")
38+
39+
# Setting email
40+
msg = EmailMessage()
41+
msg.set_content(args.body.read())
42+
msg['Subject'] = args.title
43+
msg['From'] = from_addr
44+
45+
# Sending emails
46+
with SMTP_SSL(host=host, port=port) as smtp:
47+
smtp.login(from_addr, password)
48+
49+
print("Sending email to: %s" % (args.addr_list))
50+
msg['To'] = args.addr_list
51+
smtp.send_message(msg)

0 commit comments

Comments
 (0)