Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

nagios https proxy monitoring plugin or Bash website monitoring script

#!/bin/bash

if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
fi

if [ $# -eq 1  ]
then

output=`curl  -x 10.11.10.10:3128 -L $1 -m 10 -o /dev/null || echo "Website_down"`

if [ "$output" == "Website_down" ] ; then

echo "Website is down CRITICAL  "
exit 2

else

echo "Website_up $1  OK"
exit 0
fi

fi

Monitoring of kibana and logstash services and start them if process not found

Kibana and logstash might run out of memory and application might get closed or get terminated. In that case we will be running the below script to monitor services and start them if not running .
There are other parameters that need to be verified before going for this option like java heap size etc.


#!/bin/bash

LOGSTASH=logstash
KIBANA=kibana


var1=`ps -ef|grep -i $LOGSTASH|sed -e '/grep/d'`

if [ -z "$var1" ]; then

nohup /opt/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf &

echo "Process logstash not found" | mail -s "Process logstash not found creating a new one" devops@learnadmin.com

else

echo "process logstash  found."

fi

var2=`ps -ef|grep -i $KIBANA|sed -e '/grep/d'`

if [ -z "$var2" ]; then

nohup /opt/kibana/bin/kibana status &


echo "Process kibana  not found" | mail -s "Process kibana not found creating a new one" devops@learnadmin.com

else

echo "process kibana found."


fi

How to parse xml tags in bash or retrieve xml tag values in linux


Consider the following example file test.xml





<header>
        <tag1>1</tag1>
        <system>test123</system>
        <cdc>230</cdc>
       
</header>

   
 


 grep -E -m 1 -o "(.*)" test.xml

This will return "test123"


if you want only the value we need to pipe it further

grep -E -m 1 -o "(.*)" test.xml | sed -e 's,.*\([^<]*\).*,\1,g'

This will return "test123"

Bash script to mask credit card number or any data in logs.

create a temp test file for testing your data

cat test.txt

hi this is my credit card number 1234567891234567
i have 22 this 22 is just for testing 1234567890123456 and this one

Here is the format which varies according to your requirement .
In my case there are 16 digits . 
I want to mask first 6 number here is the regular expression or bash command 


 sed -e :a -e "s/[0-9]\{6\}\([0-9]\{10\}\)/\*\*\*\*\*\*\1/;ta" test.txt 


here 6 and 10 are number of masks and number of visible characters respectively.
you can alter them according to your needs. 

And the stars can be replaced by # or the value you want to mask with .
The number of starts should be equal to number of masks in my case it is 6

Linux bash script for checking and sending email alerts on disk space issues

#!/bin/sh
FILE=/tmp/mailcontent.txt

> /tmp/mailcontent.txt

df -H | awk '{ print $5 " " $6 }'|head -6|tail -5|while read output;

do
#  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
echo $usep
  if [ $usep -ge 90 ]; then
   echo -e "Running out of space \n\nPartition:\"$partition ($usep%)\" \nHost:$(hostname) \nDate:$(date)\n" >> /tmp/mailcontent.txt

 fi

done

if [[ -s $FILE ]] ; then

mail -s "CRITICAL ALERT:Disk space of $hostname more that 90%" disk-alert@yourcompany.com
else
echo "$FILE is empty."
fi

exit

Get CPU RAM MEMORY of all the servers on the network


In order to accomplish this you have to have an account with password less login for all the server on the network you want to retrieve data

Here is my tutorial for password less login

Create a file "ips" which should contain all the ip's you want to retrive data for ( 1 ip per line )


for i in `cat ips`;do ssh -t $i 'echo -e -n `uname -n `"   '$i' " ;cat /proc/cpuinfo | grep "cpu cores" | uniq';done > cpucores

( This one will not work in single core cpu's or vm's )

for i in `cat ips`;do ssh -t $i 'echo -e -n `uname -n `"   '$i'" ;grep MemTotal /proc/meminfo   ';done > Mem_details


for i in `cat ips`;do ssh -t $i 'echo -e -n `uname -n `"   '$i'  " ; sudo /sbin/fdisk -l | grep Disk ';done >hdd_details 

( This one will not work in single core cup's or vm's )

After you run all the three commands you will have three files in your directory

1) cpucores - list servername ip-address and number of cores
2) Mem_details - list servername ip-address and memory details in kb
3) hdd_details - list servername ip-address and drive and size.

Bash script to find average file size in a directory

If you want output in Kb


 ls -l | awk '{s+=$5} END {print "Average file size: " s/NR/1024 "k"}'
Average file size: 28683.4k

If you want output in MB
ls -l | awk '{s+=$5} END {print "Average file size: " s/NR/1024/1024 "MB"}'
Average file size: 28.0112MB

If you want output in GB

ls -l | awk '{s+=$5} END {print "Average file size: " s/NR/1024/1024/1024 "GB"}'
Average file size: 0.0273546GB



Bash Script to get public ip

#!/bin/bash




pubip=$( curl http://ip4.me 2>/dev/null | sed -e 's#<[^>]*>##g' | grep '^[0-9]' )



echo $pubip



you can replace curl command with



curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' 

Bash script to check command-line arguments and set default if not present

if [ -n "$1" ]
# Test whether command-line argument is present (non-empty).
then
lines=$1
else
lines=$LINES # Default, if not specified on command-line.
fi

Script to check weather user is root or not

if [ "$UID" -ne "$ROOT_UID" ]
then
 echo "Must be root to run this script."
 exit $E_NOTROOT
fi

Script to check weather user is root or not

if [ "$UID" -ne "$ROOT_UID" ]
then
 echo "Must be root to run this script."
 exit $E_NOTROOT
fi

Bash Script to find CPU type or model

#!/bin/sh

if [ -f /proc/cpuinfo ]; then

    echo -n "MODELNAME=\""
    grep -m 1 "model name" /proc/cpuinfo | cut -d: -f2 | sed -e 's/^ *//' | sed -e 's/$/"/'
   
fi



OUTPUT:
MODELNAME="Intel(R) Core(TM)2 Duo CPU     E8200  @ 2.66GHz"

Bash Script to find system architecture

#!/bin/sh

echo ARCH=`uname -m`



OUTPUT:


ARCH=x86_64