Skip to content

Commit 260f5ae

Browse files
committed
Initial commit
0 parents  commit 260f5ae

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed

check_disk_space.sh

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/bin/sh
2+
3+
# check_disk_space.sh
4+
#
5+
# Description:
6+
# Checks for available disk space and sends warning (via email) if below
7+
# urgent/critical thresholds. For use on outdated servers that weren't properly
8+
# maintained by previous 'owners' that are running on legacy/end-of-life
9+
# operating system(s). On a properly maintained system, there are far more
10+
# elegant ways to monitor disk space. The author(s) recommend you use those
11+
# other methods instead of this software if/where at all possible.
12+
#
13+
# Usage: Install to location of your choice and setup a cronjob as root.
14+
#
15+
# Example: 00 12 * * * /root/sbin/check_disk_space.sh
16+
#
17+
# Author : Omar Asfour <[email protected]> https://omar.asfour.ca
18+
# License : CC0 1.0 Universal (CC0 1.0) - Public Domain Dedication
19+
# License URL: http://creativecommons.org/publicdomain/zero/1.0/
20+
#
21+
# To the extent possible under law, the author(s) have dedicated all copyright
22+
# and related and neighboring rights to this software to the public domain
23+
# worldwide.
24+
#
25+
# This software is distributed without any warranty. You should have received a
26+
# copy of the CC0 Public Domain Dedication along with this software.
27+
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
28+
#
29+
30+
### CONFIGURATION ###
31+
urgent=2048 # urgent threshold (in MiB - Mebibytes available))
32+
critical=1024 # critical threshold
33+
34+
mailhost=10.0.0.2
35+
mailport=25
36+
fromname="Server"
37+
38+
toname="recipient"
39+
40+
41+
### SUPPORTING FUNCTIONS ###
42+
43+
# Checks last status line returned by server.
44+
# By default, expects '250' status; but can be invoked
45+
# to check for other status codes
46+
# eg. checkStatus "${sts}" "${line}" 220
47+
function checkStatus {
48+
expect=250
49+
if [ $# -eq 3 ] ; then
50+
expect="${3}"
51+
fi
52+
if [ $1 -ne $expect ] ; then
53+
echo "Error: ${2}"
54+
exit
55+
fi
56+
}
57+
58+
# Establishes connection to server and sends message
59+
# Parameters:
60+
# $1 = status (string)
61+
# $2 = space_available (integer)
62+
# eg. sendWarning 'critical' 1234
63+
function sendWarning {
64+
msgdate=$(date +"%a, %d %b %Y %T %z")
65+
msgstatus=$(echo "$1" | tr '[:lower:]' '[:upper:]')
66+
subject="Server Disk Space $msgstatus"
67+
message="Disk space $1: $2 MiB"
68+
69+
# Open TCP/UDP Socket using file-descriptor '3'
70+
exec 3<>/dev/tcp/${mailhost}/${mailport}
71+
72+
# Connect to SMTP Server
73+
read -u 3 sts line
74+
checkStatus "${sts}" "${line}" 220
75+
echo "HELO ${mailhost}" >&3
76+
77+
read -u 3 sts line
78+
checkStatus "$sts" "$line"
79+
echo "MAIL FROM: <${fromaddr}>" >&3
80+
81+
read -u 3 sts line
82+
checkStatus "$sts" "$line"
83+
echo "RCPT TO: <${toaddr}>" >&3
84+
85+
read -u 3 sts line
86+
checkStatus "$sts" "$line"
87+
echo "DATA" >&3
88+
89+
read -u 3 sts line
90+
checkStatus "${sts}" "${line}" 354
91+
92+
# Send Payload (message)
93+
echo "Date: $msgdate" >&3
94+
echo "From: $fromname <$fromaddr>" >&3
95+
echo "To: $toname <$toaddr>" >&3
96+
echo "Subject: $subject" >&3
97+
echo "$message" >&3
98+
echo "." >&3
99+
100+
# Confirm Success & Quit
101+
read -u 3 sts line
102+
checkStatus "$sts" "$line"
103+
echo "QUIT" >&3
104+
105+
# Confirm Quit(ted) successfully -- Not really necessary
106+
read -u 3 sts line
107+
checkStatus "${sts}" "${line}" 221
108+
}
109+
110+
### MAIN (script entry point) ###
111+
112+
# Get available MiB on / mount-point
113+
available=`df -m | grep '/$' | awk '{print $3}'`
114+
115+
# Compare avaialble disk space against thresholds and
116+
# send warning if below threshold
117+
if [[ $available -lt $critical ]]; then
118+
sendWarning 'critical' $available
119+
elif [[ $available -lt $urgent ]]; then
120+
sendWarning 'urgent' $available
121+
fi

cleanmacfiles.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
################################################################################
3+
# Author: Omar Asfour <http://omar.asfour.ca>
4+
# Release Date: 2013-12-19
5+
# Version: 0.0.1
6+
#
7+
# To the extent possible under law, the author(s) have dedicated all copyright
8+
# and related and neighboring rights to this software to the public domain
9+
# worldwide. This software is distributed without any warranty.
10+
#
11+
# You should have received a copy of the CC0 Public Domain Dedication along with
12+
# this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>
13+
################################################################################
14+
# NAME: cleanmacfiles
15+
# PURPOSE: Fairly obvious, I'd think. Gets rid of files created by Mac computers
16+
# that can be a little annoying.
17+
# CAUTION: In some cases, those bothersome Mac files are 'necessary'.
18+
# For example; do not use this on your Time Machine backups.
19+
# You'll regret it.
20+
# USAGE: cleanmacfiles.sh [path]
21+
# OPTIONS: path : defaults to current directory
22+
#
23+
# Note: Execution is recursive. Using this will purge files in subdirectories
24+
################################################################################
25+
#
26+
# Finds files Macs love to throw all over network shares and external media
27+
# and destroys them
28+
function macFilesMustDieIn {
29+
(for target in {Temporary\ Items,Network\ Trash\ Folder,.{DS_Store,AppleDB,TemporaryItems,AppleDouble,bin,AppleDesktop,Spotlight,Trashes,fseventd,_*}}; do
30+
find $1 -name "$target" -print0;
31+
done) | xargs -0 -I {} rm -rvf {}
32+
}
33+
34+
# Command Line Parser
35+
if [ -n "$1" ]; then # Executes on directory given in first argument
36+
macFilesMustDieIn $1
37+
else # If no argument given, executes on current working directory
38+
macFilesMustDieIn .
39+
fi

ipdiffmail.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
################################################################################
3+
# ipdiffmail.sh - IP Address Change Email Notifier
4+
#
5+
# Author: Omar Asfour <http://omar.asfour.ca>
6+
# Release Date: 2013-12-19
7+
# Version: 0.0.1
8+
#
9+
# To the extent possible under law, the author(s) have dedicated all copyright
10+
# and related and neighboring rights to this software to the public domain
11+
# worldwide. This software is distributed without any warranty.
12+
#
13+
# You should have received a copy of the CC0 Public Domain Dedication along with
14+
# this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>
15+
################################################################################
16+
# Purpose: Check for a change of your external IP address, and send you an
17+
# email in the event of a change.
18+
#
19+
# WARNING! This script will contain your email login info. Lock it down.
20+
#
21+
# Dependency: cURL <http://curl.haxx.se/>
22+
# Dependency: SendEMail <http://caspian.dotconf.net/menu/Software/SendEmail/>
23+
#
24+
# Requirement: SMTP/TLS server
25+
# Requirement: user write permission to path of $IPDIFFCURR (default /root/tmp)
26+
# Requirement: user execute permission on /bin/hostname
27+
#
28+
# Exit 0: IP Address not changed
29+
# Exit 1: IP Address changed ; SendEmail invoked (ie. check your inbox)
30+
################################################################################
31+
# Usage Notes:
32+
#
33+
# To have this run every 12 hrs on the 30 min mark, and log only changes
34+
# (stderr) to syslog, add the following to **crontab** (5), substituting [user]
35+
# for the file owner, and [path] with the path to this script:
36+
#
37+
# 30 */12 * * * [user] ([path]/ipdiff.sh > /dev/null) 2>&1 | logger -i -t IPDIFF
38+
#
39+
################################################################################
40+
DEBUG=0 # Set to 1 for debug mode
41+
IPDIFFCURR='/root/tmp/ipdiff.curr' # Tracks current/last-known IP
42+
SENDERNAME=`/bin/hostname` # Sender (ie. From: in email)
43+
SMTPSERVER='smtp.yourserver.com:587' # SMTP/TLS server:port
44+
45+
# Modify these to match your Gmail account.
46+
RECEIVERNAME='Your Name' # Your name
47+
SMTPUSER='yoursmtpaccount' # Your SMTP/TLS account
48+
SMTPPASS='y0urp@sswd' # Your SMTP/TLS password
49+
50+
# Sends notification email and updates record of current IP Address
51+
function change_execute {
52+
MSGBODY="$SENDERNAME IP Address changed: $CURRIP $NEWIP"
53+
`/usr/bin/sendemail -s $SMTPSERVER -f "$SENDERNAME <$SMTPUSER>" -t "$RECEIVERNAME <$SMTPUSER>" -u $MSGBODY -m $MSGBODY -o tls=yes -o username=$SMTPUSER -o password=$SMTPPASS`
54+
echo $NEWIP > $IPDIFFCURR
55+
}
56+
57+
# IP Address has changed. Call change_execute, and write message to 'standard error', exit in 'error' state.
58+
function change_exit {
59+
change_execute
60+
echo "$1" 1>&2
61+
exit 1
62+
}
63+
64+
# IP Address has not changed. Write message to 'standard output' and exit in 'success' state.
65+
function nochange_exit {
66+
echo "$1"
67+
exit 0
68+
}
69+
70+
# Checks for IP Address change, relying on opendns.com for answers
71+
function ipchange_check {
72+
NEWIP=`nslookup -query=a myip.opendns.com resolver1.opendns.com | awk -F': ' 'NR==6 {print $2}'`
73+
if [ $DEBUG -gt 0 ]; then echo "\$NEWIP=$NEWIP"; fi
74+
if [ $DEBUG -gt 0 ]; then echo "\$CURRIP=$CURRIP"; fi
75+
if [ $NEWIP != $CURRIP ]; then
76+
change_exit "IP Address changed: $CURRIP $NEWIP"
77+
else
78+
nochange_exit "IP Address unchanged: $CURRIP/$NEWIP"
79+
fi
80+
}
81+
82+
# MAIN (Script Entry Point)
83+
if [ -f $IPDIFFCURR ]; then
84+
CURRIP=`cat $IPDIFFCURR`
85+
ipchange_check
86+
else
87+
CURRIP='0.0.0.0'
88+
ipchange_check
89+
fi

0 commit comments

Comments
 (0)