BASH redirection reference

# redirect stdout to file
cmd > file
# redirect stderr to file
cmd 2> file
# redirect both stderr and stdout to file
cmd >& file
# pipe cmd1′s stdout to cmd2′s stdin
cmd1 | cmd2
# pipe cmd1′s stdout and stderr to cmd2′s stdin
cmd1 2>&1 | cmd2
# print cmd1′s stdout to screen and also write to file
cmd1 | tee file
# print stdout and stderr to screen while writing to file
cmd1 2>&1 | tee file

Posted in Scripts, Linux | Tagged , , , , , , | Leave a comment

Configure iSCSI with Multipathing on RHEL 6

Reblogged from Infrastructure Adventures:

I recently had to configure iSCSI with multipathing on RHEL 6. It wasn’t too hard and these instructions will presumably work with other Redhat based versions & distros

It involves installing/configuring the iSCSI utilities and then installing/configuring Device-Mapper. Device-Mapper will automatically discover devices with multiple paths and creates a mpath device that can be used to load balance/failover between all the paths.

Read more… 747 more words

Posted in Uncategorized | Leave a comment

Python – writing to a file on a fileystem to test I/O

This is very useful Python module that will allow you to test writing to a file on a filesytem. This can be used to verify that their are no I/O read or write failures during an extend or migration of a filesystem.

# loops through a test file, opens file, writes text, closes file and repeats “n” number of times

import time
testfile = “/datatest/testfile.txt”

for i in range(100):
    print i
    fo = open(testfile, “a”)
    fo.write(“this is a test of writing to a file while the filesystem is being extended\n”);
    print fo.name
    fo.close()
    time.sleep(0.2)

Posted in Uncategorized | Tagged , , , , , | Leave a comment

Combining multi-line events into single transactions in Splunk

This can be used to group together multiple lines, of which belong to the same thread. You can pipe the data to a “transaction startswith/endswith” as described below, and Splunk will group the lines into a single shared “transaction”. Then the “table” command basically creates a table of the output of the field.

NOTE: This is slow and very taxing for Splunk, so do it against a short time frame, and don’t schedule any reports to do this (the “transaction” function).

source=”/source/to/data” (host=”server1″ OR host=”server2″) | transaction startswith=”starting word” endswith=”ending word” | search field1 | table field2

Posted in Splunk | Tagged , , , | Leave a comment

Joining RedHat Servers to Active Directory

Joining Redhat servers to AD domain
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

yum install samba3x
yum install winbind

vi /etc/nsswitch

=================================
passwd: files winbind
shadow: files winbind
group: files winbind
=================================

vi /etc/samba/smb.conf

===================================
workgroup = DOMAINNAME
password server = x.x.x.x
realm = DOMAINNAME.COM
security = ads
idmap uid = 10000-20000
idmap gid = 10000-20000
winbind separator = +
template homedir = /home/%D/%U
template shell = /bin/bash
winbind use default domain = false
winbind offline logon = false
====================================

net ads join -U user@domainname.com

Posted in Uncategorized | Leave a comment

Backup and Archive Nagios

This is a shell script that can be used to backup a Nagios configuration (or any group of files/directories in Linux or UNIX) locally, and to sync the backups to a remote location. This script works perfectly when there are two different Nagios instances in different locations, and this script can be used on both servers to backup and archive, and then rsync the files to the remote side – just by changing the three variables at the top of the script. Logging and emailing results of each job can be added in to the script as well.

A best practice that I implemented is to use SSH shared keys for the rsync. Use a non-root account and send the traffic along a trusted VLAN. This allows for the SSH to not prompt for a password every time the script is run, which should be automated through a cron job.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/bash
# nagios-backups.sh

# Dennis Miller
# March 29 2011

####################################
###### Local System Variables ######
####################################

NAGIOS=usr/local/nagios
LOCAL=/local/directory/path
REMOTE=user@server:/local/directory/path

####################################
####### DO NOT CHANGE BELOW ########
####################################

BACKUP=$LOCAL/nagios-backup.tgz
DATE=`date +”%F-%T”`

export LOCAL
export REMOTE

### check to see if current backup file exists ###
if [ -f $BACKUP ]
then
echo “Backup file exists.”
mv $BACKUP $BACKUP-$DATE
tar czf $BACKUP -C / $NAGIOS
else
echo “Backup file does not exist…creating.”
tar czf $BACKUP -C / $NAGIOS
exit
fi

### remove files older than seven days ###
find $LOCAL -type f -mtime +7 -exec rm {} \;

### change the permissions of the file to the backups user ###
chown -R backups:backups $LOCAL

### change to backups user to run the rsync script ###
su backups -c /home/backups/rsync-files.sh

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/bash
# rsync-files.sh

# Dennis Miller
# March 29 2011

### rsync the files to the DR backup site ###
rsync -avz –delete $LOCAL/ $REMOTE

Posted in Linux, Nagios, rsync, Scripts, SSH | Leave a comment

Script for emailing DFS Replication Health Reports

DFS replication is a great way to synchronize data for DR purposes, but there is no built in scheduled reporting mechanism.  Well here is a script I wrote that runs the dfrsadmin reports and attaches each report, as well as sends links to each report for review.  Very helpful considering I was logging in each day and running commands to check the backlog.  Now, I can just open these reports every day and all the information is right there.  Once the script is there, simply create a scheduled task to run this script at whatever time interval is needed to receive these reports.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:: Script to generate and email DFS Replication Health Reports.
:: Dennis Miller
:: November 10 2010

@echo off

set CURRDATE=%TEMP%\CURRDATE.TMP
set CURRTIME=%TEMP%\CURRTIME.TMP
set REPORTS=\\*******\*******
set FROM=”DFS Replication <******@*******>”
set TO=”******* <*******@*******>”

DATE /T > %CURRDATE%
TIME /T > %CURRTIME%

:: This cleans up old reports to conserve space.

FORFILES /p E:\****************** /m *.* /d -30 /c “cmd /c del @FILE”

:: This adds the date and time to the health report name and to the title of the email.

for /F “tokens=1,2,3,4 delims=/, ” %%i in (%CURRDATE%) Do SET DDMMYYYY=%%j-%%k-%%l
for /F “tokens=1,2,3 delims=:, ” %%i in (%CURRTIME%) Do Set HHMM=%%i%%j%%k

:: Here you define the different resources by specifying the name of each replication group, server name and
:: share name.  You can do this for as many replication groups as you want, but keep in mind that the script
:: will wait until all reports complete before mail is sent out.

set RG1_Report=%REPORTS%\folder1-%DDMMYYYY%-%HHMM%.html
set RG2_Report=%REPORTS%\folder2-%DDMMYYYY%-%HHMM%.html
set RG3_Report=%REPORTS%\folder3–%DDMMYYYY%-%HHMM%.html
set RG4_Report=%REPORTS%\folder4-%DDMMYYYY%-%HHMM%.html

:: Here you define the report options as specified in the dfrsadmin.exe utility.

dfsradmin health new /rgname:folder1 /refmemname:server1 /ReportName:%RG1_Report% /fscount:true
dfsradmin health new /rgname:folder2 /refmemname:server1 /ReportName:%RG2_Report% /fscount:true
dfsradmin health new /rgname:folder3 /refmemname:server2 /ReportName:%RG3_Report% /fscount:true
dfsradmin health new /rgname:folder4 /refmemname:server3 /ReportName:%RG4_Report% /fscount:true

:: Here is where the current report file names get stored into this temp file location, overwritten each time.

echo folder1 %RG1_Report% > %TEMP%\healthMessageBodyRG1.txt
echo folder2 %RG2_Report% > %TEMP%\healthMessageBodyRG2.txt
echo folder3 %RG3_Report% > %TEMP%\healthMessageBodyRG3.txt
echo folder4 %RG4_Report% > %TEMP%\healthMessageBodyRG4.txt

:: This is for the links to all of the report file locations to show up in the body of the message.

echo folder1 %RG1_Report% > %TEMP%\healthMessageBody.txt
echo folder2 %RG2_Report% >> %TEMP%\healthMessageBody.txt
echo folder3 %RG3_Report% >> %TEMP%\healthMessageBody.txt
echo folder4 %RG4_Report% >> %TEMP%\healthMessageBody.txt

:: This is for the individual reports to be sent as attachments.

for /F “tokens=2 delims= ” %%i in (%TEMP%\healthMessageBodyRG1.txt) Do SET FILESRG1=%%i
for /F “tokens=2 delims= ” %%i in (%TEMP%\healthMessageBodyRG2.txt) Do SET FILESRG2=%%i
for /F “tokens=2 delims= ” %%i in (%TEMP%\healthMessageBodyRG3.txt) Do SET FILESRG3=%%i
for /F “tokens=2 delims= ” %%i in (%TEMP%\healthMessageBodyRG4.txt) Do SET FILESRG4=%%i

:: Here is the command to email the links as well as the attachments using the sendEmail.exe utility.

sendEmail.exe -f %FROM% -t %TO% -u “DFS Replication Health Reports %DDMMYYYY%” -o message-file=%TEMP%\healthMessageBody.txt -s smtpserver.domain.com -a %FILESRG1% %FILESRG2% %FILESRG3% %FILESRG4%

Posted in DFS Replication, Scripts | Tagged , , , , | Leave a comment