Author Archives: MasonCloud

Get Notified When Users Run “sudo”

Published / by MasonCloud / Leave a Comment
# visudo

Paste the following into your config:

###### Get every alert when user fired an command with sudo ####
Defaults        syslog=auth, insults, syslog_goodpri=alert
Defaults        logfile=/var/log/sudo.log
Defaults        timestamp_timeout=0, log_year, tty_tickets
Defaults        mailto="@YourDomain.com", mail_always, mail_badpass, mail_no_user

Process Checker (Running or Not?)

Published / by MasonCloud / Leave a Comment
#!/bin/bash
# Name : service.chk
# URL: https://bash.cyberciti.biz/monitoring/simple-process-checker-script/
# Purpose: A simple process checker. Find out if service is running or not.
# Tested on: Debian and RHEL based system only.
# ----------------------------------------------------------------------------
# Author: nixCraft http://www.cyberciti.biz/
# Copyright: 2009 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 13/Mar/2013 - Added support for email and other enhancements
# Last updated: 05/Dec/2011 - Added support for binary path check
# ----------------------------------------------------------------------------
# Runn the script as below to only show "running" processes
# clear ; ./process_checker.sh | grep -v "not running"
# ----------------------------------------------------------------------------
#
echo "Service status on ${HOSTNAME} @ $(date)"
echo "------------------------------------------------------"
sleep 1 ; chkconfig --list
echo "------------------------------------------------------"
## Change as per your distro
_pgrep="/usr/bin/pgrep"
_mail="/usr/bin/mail"
## Add binary list here
_chklist="/usr/bin/php-cgi /usr/sbin/mysqld /usr/sbin/apache2 /usr/sbin/nginx /usr/sbin/httpd"
#_chklist="/usr/bin/php-cgi /usr/sbin/lighttpd /usr/sbin/named /usr/sbin/pgsqld /usr/sbin/mysqld /usr/sbin/apache2 /usr/sbin/nginx /usr/sbin/httpd"
## yes | no
_sendemail="no"
## Add your email id
_email="your@mobile.email.id.example.com"
## Do not change below
_failed="false"
_service="Service:"
_running() {
 local p="${1##*/}"
 local s="true"
 $_pgrep "${p}" >/dev/null || { s="false"; _failed="true"; _service="${_service} $1,"; }
 [[ "$s" == "true" ]] && echo "$1 running" || { echo -n "$1 not running"; [[ ! -f "$1" ]] && echo " [ $1 not found ]" || echo ; }
}
## header
echo "Service status on ${HOSTNAME} @ $(date)"
echo "------------------------------------------------------"
## Check if your service is running or not
for s in $_chklist
do
 _running "$s"
done
## Send a quick email update (good for cron jobs) ##
[[ "$_failed" == "true" && "$_sendemail" == "yes" ]] && { _mess="$_service failed on $HOSTNAME @ $(date)";
 $_mail -s 'Service not found' "$_email" < "${_mess}";
 }

Create AMI from EC2 Image

Published / by MasonCloud / Leave a Comment
# vim /root/bin/ami-create.sh
------

#!/bin/bash

#
# @Purpose Creates an image (AMI) of the given EC2 instance
# @Background Meant to be run as a cronjob. Requires that awscli is installed. Assumes that the
# instance running this command has the permission ec2:CreateImage assigned via IAM.
#
# @Usage: ec2-create-image <instance-id>
#

DATE=$(date +%Y-%m-%d_%H-%M)
AMI_NAME="WordPress Backup - $DATE"
AMI_DESCRIPTION="WordPress Backup - $DATE"
INSTANCE_ID=$1

printf "Requesting AMI for instance $1...\n"
aws ec2 create-image --instance-id $1 --name "$AMI_NAME" --description "$AMI_DESCRIPTION" --no-reboot

if [ $? -eq 0 ]; then
 printf "AMI request complete!\n"
 fi

------
# /root/bin/ami-create.sh i-0b84b3b0de37cce1a >> /root/log/crontab.log 2>&1

------
# aws ec2 deregister-image --image-id i-0b84b3b0de37cce1a