EMC CLARiiON Monitoring – Adding TCP port variable

Modified check_emc_clariion.pl script to specify a different (variable) TCP port for EMC CLARiiON monitoring via Nagios, Cacti, or otherwise.

Nagios

I’ve been using the check_emc_clariion.pl script for a while to monitor an EMC CLARiiON SAN, but recent issues made me realize the need to use the same check for another host. The problem is the hosts are behind a firewall and using port forwarding – fine for one host on the default port (17894), but I needed to specify a different TCP port to use for the new host.

I updated the check_emc_clariion.pl script to include a TCP port variable, for use like so:

root@host:~#/usr/lib/nagios/plugins/check_emc_clariion.pl -H 192.168.100.10 -u user -p password -t faults --warn 50 --crit 70 --tcp_port=17895
The array is operating normally.

Here is a diff of the original script and my changes:

195d194
< $opt_tcp_port 214,217d212 < my $opt_tcp_port = ''; < < ### set tcp port < $opt_tcp_port=17894; 272,273c267 < 'paths=s' => $opt_pathcount,
< 'tcp_port=s' => $opt_tcp_port
---
> 'paths=s' => $opt_pathcount
1056c1050
< open( NAVICLIOUT, "$NAVICLI_CMD -h $opt_host -Port $opt_tcp_port Faults -list |" ); --- > open( NAVICLIOUT, "$NAVICLI_CMD -h $opt_host -Port 17894 Faults -list |" );

EMC CLARiiON - Nagios

EMC CARiiON - Status Detail

EMC CLARiiON - Cacti graph

Share

Bash Scripting repetitive tasks – while read x; do

Use a “while loop” type bash script for repetitive Linux tasks

More “while loop” bash scripting examples at tldp.org

An audit on a Linux server made it necessary to get information regarding every user on the machine, and match those users to a mail directory (which may or may not have a different name). Instead of running the ‘finger’ command three hundred times and copying the output somewhere I used this:
#! /bin/sh
while read username; do
finger ${username}
done < inputfile > outputfile
exit 0

I saved this as finger_script.sh and copied each of the usernames to the file ‘inputfile’. The ‘inputfile’ contained just the usernames that I cut and pasted from the mail directory in question, and looked like this:
bill frank keith user01 user05 user09
carl gary larry user02 user06 user10
dan heather mary user03 user07 user11
denise joe nancy user04 user08 user12

Running finger_script.sh ran the finger command on each user in the ‘inputfile’ and immediately output a list of users that did not exist. It also created ‘outputfile’, which contained a list of the information on each user:
Login: bill Name: Bill Person
Directory: /home/b/bill Shell: /bin/nologon
Never logged in.
No mail.
No Plan.
Login: carl Name: Carl Guy
Directory: /users/carl Shell: /bin/nologon
Never logged in.
No mail.
No Plan.
Login: dan Name: Dan Theman
Directory: /users/dan Shell: /bin/nologon
Never logged in.
No mail.
No Plan.

Share

Log Off Users from Terminal Server – .bat File

Log off all users, but with specific exceptions

RDP - Remote Desktop Protocol

I came across a situation where we needed to automatically disconnect all users from a terminal server, except for one specific user. The following script (thanks Ferdinand!) can be run as a batch file to accomplish just that; it will log off all terminal server users except for ‘userx’. It gives each user a 5 minute and 1 minute warning, then runs a ‘query session’ to see what sessions show up and writes them to a file call ‘sessions.txt’. Next it searches the session.txt file for “userx” and writes the rest of the sessions to “killts.txt”, logs off those sessions/users, and deletes the .txt files that it created. It goes through the process twice to get active AND disconnected sessions.


@ECHO OFF
msg * You will be logged off in 5 minutes.
choice /T:240 /D N /N > Nul
msg * You will be disconnected in 1 minute! Please log off now!
choice /T:60 /D N /N > Nul
query session >C:SchedTaskssessions.txt
find /v "userx" C:SchedTaskssessions.txt > C:SchedTaskskillts.txt
for /f "skip=5 tokens=3," %%i in (C:SchedTaskskillts.txt) DO logoff %%i
query session >C:SchedTaskssessions.txt
find /v "userx" C:SchedTaskssessions.txt > C:SchedTaskskillts.txt
for /f "skip=5 tokens=2," %%i in (C:SchedTaskskillts.txt) DO reset session %%i
del C:SchedTaskssessions.txt
del C:SchedTaskskillts.txt
EXIT

Here are some other related Windows TS/RDS commands (from thelazyadmin.com):

Query TermServer – Lists all terminal servers in the current domain.
QUERY TERMSERVER [/domain:domain] [/address][/continue]
* /domain:domain – specifies the domain (current logged on domain is default)
* /address – lists the IP address of the terminal server
* /continue – removes the pause between output screens

Query Session – Lists all current sessions running on a terminal server.
QUERY SESSION [sessionname | username | sessionid][/server:servername] [/mode] [/flow] [/connect] [/counter]
* sessionname is the name of the session that you want to query
* username is the name of the user you want to query
* sessionid is the ID of the session you want to query
* /server:servername is the name of the server you are querying
* /mode outputs the current line settings
* /flow outputs the current flow control settings /connect outputs the current connection settings
* /counter outputs the counter information for the server

Query User or Quser – Lists all current users on a terminal server
QUERY USER [username | sessionname | sessionid] [/server:servername]
* sessionname is the name of a specific session that you want to query
* username is the name of the specific user you want to query
* sessionid is the ID of the specific session you want to query
* /server:servername is the name of the server you are querying

Query Process – Lists all processes running on the terminal server.
QUERY PROCESS [ x | processid | username | sessionname | /id:nn | programname] [/server:servername] [/system]
* x lists information on all processes (note – replace x with an asterisk)
* processid lists information about only the specific process ID
* username lists processes running under the context of a specific user
* sessionname lists processes running under the context of a specific session
* /ID:nn lists processes running in the session with the specified session ID number
* programname lists all processes started by the specified executable
* /server:servername is the name of the server you are querying—the default is the server you are logged on to
* /system lists processes running under the system context

TSShutdn – Will shutdown/reboot the terminal server after a specified delay.
TSSHUTDN [wait_time] [/server:servername] [/reboot] [/powerdown] [/delay:logoffdelay] [/v]
* wait_time is the number of seconds to wait after notifying the users that the terminal server is about to shut down before forcibly logging them off (the default is 30 seconds)
* /server:servername is the name of the server to reboot/shutdown (the default is the server to which you are connected)
* /reboot reboots the server
* /powerdown powers down the server after Windows has shutdown; the servers BIOS must support this command
* /delay:logoffdelay the number of seconds to wait after logging out all users before shutting down the system (the default is 30 seconds)
* /v displays verbose information about actions being performed

Logoff – Will logoff the specified user off the terminal server and close the session. Caution, if you don’t specify a user it will log you off!
LOGOFF [sessionid | sessionname] [/server:servername] [/v]
* sessionid is the ID of the session you want to logoff
* sessionname is the name of the session you want to logoff
* /server:servername specifies the name of server on which the session you want to logoff is running
* /v displays verbose information about actions being performed

Reset Session – Will kill the specified users session without warning which can be useful when a users session is stuck. Caution, if you don’t specify a user it will kill your session!
RESET SESSION [sessionname | sessionid] [/server:servername] [/v]
* sessionid is the ID of the session you want to logoff
* sessionname is the name of the session you want to logoff
* /server:servername specifies the name of server on which the session you want to logoff is running
* /v displays verbose information about actions being performed

MSG – Will popup a message on the specified user(s) terminal server session.
MSG [username | sessionname | sessionid | @filename | x ][/server:servername] [/time:seconds] [/v] [/w] message
* username is the name of the user to whom you are sending the message
* sessionname is the session name to which you want to send the message
* sessionid is the ID number of the session to which you want to send the message
* @filename is the name of a text file containing usernames, sessionnames, or session IDs to which you want to send the message
* x sends the message to all users on the current or specified server (note – replace x with an asterisk)
* /server:servername specifies the server where recipients of the message are connected
* /time:seconds the number of seconds to display the message before the popup closes itself
* /v displays information about the message as it is sent
* /w causes the popup window to wait for the user to click OK before closing message is the text of the message to send

Shadow – Will allow you to shadow or take control of a users session.
SHADOW [sessionname | sessionid] [/server:servername] [/v]
* sessionid is the ID of the session you want to logoff
* sessionname is the name of the session you want to logoff
* /server:servername specifies the name of server on which the session you want to logoff is running
* /v displays verbose information about actions being performed

*** NOTE: Windows Server 2008 changed the name Terminal Services (TS) to Remote Desktop Services (RDS), but the above commands are the same. ***

Share

Scalix – Command Line Shortcuts and Scripts

Show SMTP connections: lsof -i:25

Show IMAP connections: lsof -i:smtp

You should get a list like this

[root@mail ~]# lsof -i:25
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
sendmail 5138 root 4u IPv4 36311278 TCP localhost.localdomain:smtp (LISTEN)
omsmtpd 15651 root 4u IPv4 20693433 TCP mail.dounsix.local:smtp (LISTEN)

Show event log: omshowlog

Show status (-s for all services): omstat -s

Show all groups: omshowpdl –l all

Show list members: omshowpdln –l ‘listname’

Add a group: omaddpdl –l ‘groupname /node,domain/CN=groupname

Delete a group: omdelpdl –l ‘groupname /node,domain/CN=groupname

Add user to group: omaddpdln –l ‘groupname /node,domain/CN=groupname’ –n First Last’

Delete items that exceed a specified age from the Message Store for an individual:  omtidyu

Delete items that exceed a specified age from the Message Store for all users (will show folders):  omtidyallu

Show all users: omshowu -m all

Show Error Notification User: omshowenu

Set Error Notification User: omconfenu -n "William Miller/scalix,node"

Show user password controls (displays currently configured password composition, usage rules, and the password expiration time): omshowpwd

Configure password rules: omconfpwd

Show message store disk usage: sxdu
sxdu flags are as follows:
-w, --wastebasket Show the amount of space used by messages in the users wastebaskets.
-a, --all Show the total usage (excluding wastebasket) and wastebasket usage respectively.
-r, --recovery Show the amount of space used by items in the users recovery folders. (Recovery folder usage is not included in the total usage.)
-s, --summary Report only a total size.
-h, --human-readable Print sizes in human-readable format (eg 4K, 247M, 5.3G).

Clear IMAP cache: You can use sx_clearimap -u email@domain.com  if you grab the sx_clearimap script, as shown here

#!/bin/bash
usage () {
echo "usage $0 [-fdkqh] -u username"
echo ""
echo " $0 is a script to handle IMAP and IMAP-cache related problems"
echo ""
echo " -f: don't ask, just do"
echo " -d: save the old imap-cache folder and dump IMAP related entries from the eventlog to /tmp"
echo " -k: just kill the IMAP sessions, dont remove the cache-folder"
echo " -q: quiet mode"
echo " -h: help (this text)"
echo " -u: username as used by omshowu -u"
exit 1
}
while getopts ':fdkhqu:' OPTION ; do
case $OPTION in
f ) FORCE=true;;
d ) DEBUG=true;;
k ) KILLONLY=true;;
h ) usage;;
q ) QUIET=true;;
u ) NAME=$OPTARG;;
? ) echo "unknown option "-$OPTARG"."
usage ;;
: ) echo "option "-$OPTARG" needs an argument"
usage ;;
* ) usage;;
esac
done
if [ "x" == "x$NAME" ]; then
echo "-u username is required"
usage
exit
fi
REALPATH=`/opt/scalix/bin/omrealpath "~/"`
OMSHOWU=`/opt/scalix/bin/omshowu -n "$NAME" -f 2>/dev/null| grep "User Folder"`
if [ $? -eq 0 ]
then
FOLDER=`echo $OMSHOWU | awk -F/ '{print $2 "/" $3 }' `
ID=`/opt/scalix/bin/omshowu -n "$NAME" | grep "System Login :" | awk -F: '{print $2}' `
if [ "$FORCE" ]; then
DOIT=y
else
echo -n "Do you really want to remove the imap-cache for $NAME (y/N)"
read DOIT
fi
if [ "$DOIT" = "y" ]; then
if [ ! "$QUIET" ]; then echo "locking mail-account ..."; fi
/opt/scalix/bin/ommodu -o "$NAME" -K 2>/dev/null
if [ ! "$QUIET" ]; then echo "killing IMAP-processes for user $NAME (uid $ID) ..."; fi
pkill -u $ID in.imap41d
if [ ! "$KILLONLY" ]; then
if [ ! -d $REALPATH$FOLDER/imap-cache ]; then
echo "sorry, no imap-cache for mailbox $NAME"
else
if [ "$DEBUG" ]; then
if [ -f /tmp/imap-cache.tgz ]; then rm -f /tmp/imap-cache.tgz; fi
if [ ! "$QUIET" ]; then echo "storing imap-cache in /tmp/imap-cache.tgz"; fi
tar cfz /tmp/imap-cache.tgz $REALPATH$FOLDER/imap-cache 2>/dev/null
if [ ! "$QUIET" ]; then echo "saving IMAP entries in omshowlog for last 24 hours in /tmp/omshowlog-imap.txt"; fi
/opt/scalix/bin/omshowlog -p 1440 -s imap > /tmp/omshowlog-imap.txt
fi
if [ ! "$QUIET" ]; then echo "removing $REALPATH$FOLDER/imap-cache ..."; fi
rm -rf $REALPATH$FOLDER/imap-cache
fi
fi
if [ ! "$QUIET" ]; then echo "unlocking mail-account ..."; fi
/opt/scalix/bin/ommodu -o "$NAME" -k 2> /dev/null
if [ ! "$QUIET" ]; then echo "done!"; fi
fi
else
echo "can not find a mailbox $NAME"
fi

My other Scalix posts include how to install Scalix on CentOS 5, Scalix calendars, etc.

Share

Create MySql DB and Grant Permissions – Shell Script

Another script from nixCraft. This one is for creating and setting permissions on a MySql database.

#!/bin/bash
# A shell script to add mysql database, username and password.
# It can also grant remote access on fly while creating the database.
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Last updated on Jan/09/2010
# -------------------------------------------------------------------------
_db="$1"
_user="$2"
_pass="$3"
_dbremotehost="$4"
_dbrights="$5"

## Path to mysql bins ##
mysql="/usr/bin/mysql"
## Mysql root settings ##
_madminuser='root'
_madminpwd='MySQL-PassWord'
_mhost='localhost'

# make sure we get at least 3 args, else die
[[ $# -le 2 ]] && { echo "Usage: $0 'DB_Name' 'DB_USER' 'DB_PASSORD' ['remote1|remote2|remoteN'] ['DB_RIGHTS']"; exit 1; }

# fallback to ALL rights
[[ -z "${_dbrights}" ]] && _dbrights="ALL"

# build mysql queries
_uamq="${mysql} -u "${_madminuser}" -h "${_mhost}" -p'${_madminpwd}' -e 'CREATE DATABASE ${_db};'"
_upermq1="${mysql} -u "${_madminuser}" -h "${_mhost}" -p'${_madminpwd}' -e "GRANT ${_dbrights} ON ${_db}.* TO ${_user}@localhost IDENTIFIED BY '${_pass}';""

# run mysql queries
$_uamq
$_upermq1

# read remote host ip in a bash loop
# build queires to grant permission to all remote webserver or hosts via ip using the same username
IFS='|'
for i in ${_dbremotehost}
do
_upermq2="${mysql} -u "${_madminuser}" -h "${_mhost}" -p'${_madminpwd}' -e "GRANT ${_dbrights} ON ${_db}.* TO ${_user}@${i} IDENTIFIED BY '${_pass}';""
$_upermq2
done

To use the script:

./script.sh dbname username password

or

/script.sh dbname username password '192.168.1.5|192.168.1.11'
(to allow 192.168.1.5 and 192.168.1.11)

or

/script.sh dbname username password '192.168.1.5|192.168.1.11' 'SELECT,INSERT,UPDATE,DELETE'
(to allow 192.168.1.5 and 192.168.1.11 and only grant SELECT,INSERT,UPDATE,DELETE)

Share

Linux Disk Space Alerts – Cron’d Shell Script

The following steps are for setting up an email notification to alert you of low disk space on *nix boxes.

1) Write a script. I modified a script that I found at nixCraft that checks disk space and sends an email if the used space is above a specified percentage. I made some adjustments to that script, here is what I am using:


#!/bin/sh
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the used percentage
# of space is >= 90%
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# Linux shell script to watch disk space (should work on other UNIX oses )
# SEE URL: http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html
# set admin email so that you can get email
ADMIN="mailto:me@mydomain.com"
# set alert level 90% is default
ALERT=90
df -HP | grep -vE '^Filesystem|tmpfs|cdrom' |
  while read partition size used free perc mnt ;
  do
    usep=$(echo $perc | tr -d '%' )
    if [ $usep -ge $ALERT ]; then
      echo "Running out of space "$partition ($usep%)" on $(hostname) as
  on $(date)" |
       mail -s "Alert: Almost out of disk space - $usep%" $ADMIN
    fi
  done

2) Create a cron job. I simply copied the script into /etc/cron.hourly and set the permissions (chmod +x /etc/cron.hourly/diskalertscript). Alternatively, you can add it to crontab to further customize when it runs:
crontab -e
10 0 * * * /path/to/diskalertscript

Share