Friday, 6 November 2015

Concurrent connection in apache

#!/bin/bash

## Purpose:
## This script provide number of connection established with the port and type of connection


## Variable Declaration

LINE='#######################################################################################################'
tput clear
RED='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m'

## Help Menu

help()
        {
        echo -e '\033[33;36m
        USAGE:'${NC}' '$0' OPTIONS [ m | p | h ]

        -m Accepts multiport --> '$0' 80 443
        -p Accept single port --> '$0' 80
        '
        }



## Packet function

packet()
        {

        echo $LINE
        echo -e "\t\t\t${RED} PORT => ${1} $NC"
        total=`netstat -antupe |  perl -ane 'if($F[4]=~/:'${1}'/) {print}' | tee /tmp/netstat.log | wc -l`
        echo
        echo  -e "Total number of request for the  Port: ${1}  => ${GREEN}  ${total} ${NC}\n"
        echo "Detailed request for the port number: ${1}"
        cat /tmp/netstat.log | perl -ane 'print $F[5]."\n"' | uniq -c
        perl -e 'print "\n" foreach(0..2)'
        echo $LINE

        }

##Main

while getopts "p:m:h" opt
do
        case $opt in

                p)
                shift
                [[ $# -ne 1 ]] && echo "Pass single arguement " && exit
                packet $OPTARG;;
                m)
                shift
                [[ $# -lt 2 ]] && echo -e "not enough arguement\n use -p instead for single port" && exit 7
                for i in $*
                do
                        packet ${i}
                        echo
                done;;
                h)
                help;;
                *)
                echo "Generating help menu"
                help;;

        esac
done

Wednesday, 14 October 2015

Finding highest CPU utilization last 20 mins + perl + sar

sar -s `date '+%R:00' -d '20 minute ago'` | grep  -v Average | perl -ane 'if($F[@f-1] > 10) {print "Highest CPU utilization $F[0]\n"}'

Monday, 22 June 2015

Grepping file based on month + perl + one liner

# ls -l | perl -ne '@s=split(" ");if(($s[5] eq "May")&&($s[6] > 27)) {print}'

[root@centos64 ~]# ls -l | perl -ne '@s=split(" ");if(($s[5] eq "May")&&($s[6] > 27)) {print}'
-rw-r--r--  1 root root    284857 May 28 05:45 apr-1.5.0-1.x86_64.rpm
-rw-r--r--  1 root root   1072141 May 28 05:45 apr-devel-1.5.0-1.x86_64.rpm
-rw-r--r--  1 root root     77504 May 28 05:45 apr-util-1.5.3-1.x86_64.rpm
-rw-r--r--  1 root root    489168 May 28 05:45 apr-util-devel-1.5.3-1.x86_64.rpm
-rw-r--r--  1 root root   3684645 May 28 05:45 httpd-2.4.12-1.x86_64.rpm
-rw-r--r--  1 root root    203249 May 28 05:45 httpd-devel-2.4.12-1.x86_64.rpm
-rw-r--r--  1 root root   4226183 May 28 05:45 httpd-manual-2.4.12-1.x86_64.rpm
-rw-r--r--  1 root root    152418 May 28 05:45 httpd-tools-2.4.12-1.x86_64.rpm
-rw-r--r--  1 root root    129045 May 28 05:45 mod_lua-2.4.12-1.x86_64.rpm
You have mail in /var/spool/mail/root


Wednesday, 17 June 2015

Perl variabe input + stdin + arguement pass

[root@ENGWEB01T ~]# cat samd.pl

$s=( ! "$ARGV[0]" )?(<STDIN>):($ARGV[0]);
print $s."\n";

[root@ENGWEB01T ~]# perl samd.pl sam
sam
[root@ENGWEB01T ~]# perl samd.pl
sam
sam

Sunday, 14 June 2015

RPM build + spec + file

Folder Structure

[root@centos64 rpmbuild]# ls
BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS



Sources Files , should be placed inside SOURCES folder. The file inside tar ball would be placed on respective location , which specified in spec file

[root@centos64 rpmbuild]# cd SOURCES/
[root@centos64 SOURCES]# ls
s3fs-0.1.tar.gz


[root@centos64 SOURCES]# tar -ztvf s3fs-0.1.tar.gz

drwxr-xr-x root/root         0 2015-06-14 09:00 s3fs-0.1/
-rw-r--r-- root/root      1162 2015-06-14 10:05 s3fs-0.1/s3fsd
-rw-r--r-- root/root       738 2015-06-14 10:05 s3fs-0.1/s3fs
-rw-r--r-- root/root        36 2015-06-14 05:01 s3fs-0.1/s3fs.conf





Spec file:

[root@centos64 rpmbuild]# cat SPECS/sam.spec
Summary: s3fs manage services for mount points
Name: s3fs
Version: 0.1
Release: 1
URL:     http://samlinuxsam.blogspot.in/2015/06/s3fs-service-bash.html
License: GPL
Group: Applications/Internet
BuildRoot: %{_tmppath}/%{name}-root
Requires: bash
Source0: s3fs-%{version}.tar.gz
BuildArch: noarch

%description
s3fs manages mount points and check the mount points every 15 minutes

%prep
%setup

%build


%install
rm -rf ${RPM_BUILD_ROOT}
mkdir -p ${RPM_BUILD_ROOT}/usr/bin
mkdir -p ${RPM_BUILD_ROOT}/etc
mkdir -p ${RPM_BUILD_ROOT}/etc/init.d
install -m 755 s3fs ${RPM_BUILD_ROOT}%{_bindir}
install -m 755 s3fs.conf ${RPM_BUILD_ROOT}/etc/
install -m 755 s3fsd ${RPM_BUILD_ROOT}/etc/init.d/



%clean
rm -rf ${RPM_BUILD_ROOT}

%files
%defattr(-,root,root)
%attr(755,root,root) %{_bindir}/s3fs
%config /etc/s3fs.conf
%config /etc/init.d/s3fsd

%changelog
* Fri Jun 12 2015 praveensams16@gmail.com
- Uberscript!





#########




Building RPM


rpmbuild --bb rpmbuild/SPECS/sam.spec



Friday, 12 June 2015

s3fs service + bash

Service file /etc/init.d/s3fsd

#!/bin/bash

start()
        {
        if [ ! -f /var/run/s3fs_lock ]
        then
                touch /var/run/s3fs_lock
                ./usr/bin/s3fs &
                sleep 1
                if [  -f "/var/run/s3fs_lock" ]
                then
                        echo -e "\e[1;32m s3fs service  \e[0;36m [started] \e[0m"
                else
                        echo "Error in starting the service , Please check var log messages"
                fi
        else
                echo -e "\e[1;32m s3fs service \e[1;35m [Already Running] \e[0m"
                echo -e "\n Please do restart , if the service is not proper\n"
        fi
        }
stop()
        {
        if [ ! -f "/var/run/s3fs_lock" ]
        then
                echo -e "\e[1;32m s3fs service \e[1;35m [Already Stopped] \e[0m"
        else
                rm -rf /var/run/s3fs_lock
                echo -e "\e[1;32m s3fs service \e[0;36m [stopped] \e[0m"
        fi
        }

status()
        {
        if [ -f "/var/run/s3fs_lock" ]
        then
                echo -e "\e[1;32m s3fs service \e[1;33m [Running] \e[0m"
        else
                echo -e "\e[1;32m s3fs service \e[1;35m [Not Running] \e[0m"
        fi
        }


case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
exit 0



## Running code file

#!/bin/bash
while true
        do
        [ ! -f "/var/run/s3fs_lock" ] && echo "service stopped" >> /tmp/logs && kill -9 $$
        echo "`date`" >> /tmp/logs

                cat /etc/s3fs.conf | grep -v '#' | grep -v '^$' > /tmp/s3fs_tmp
                line="`wc -l /tmp/s3fs_tmp 2> /dev/null | awk '{print $1}'`"
                if [ $line -lt 1 ]
                then
                logger "/etc/s3fs.conf file is empty , please make some entry and restart the service"
                rm -rf /var/run/s3fs_lock
                exit 0
                fi

               cat /etc/s3fs.conf | grep -v '#' | grep -v '^$' | while read i
                do
                source="`echo $i | awk '{print $1}'`"
                destination="`echo $i | awk '{print $2}'`"
                check="`df -h | grep -i $destination`"

                if [ ! "$check" ]
                then
                        mount  $source $destination
                fi
                done
        sleep 5
done

## Configuration File


cat /etc/s3fs.conf
#bucktname                                      mountpoints             options
/dev/sam/sam                                    /sam


Monday, 8 June 2015

Tomcat Cookbook

#
# Cookbook Name:: tomcat
# Recipe:: default
#
# Copyright 2015, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute


tomcat_base="#{node['tomcat']['basedir']}"
tomcat_user="#{node['tomcat']['user']}"


user tomcat_user do
        shell '/bin/false'
        action :create
        not_if  "grep #{node['tomcat']['user']} /etc/passwd", :user => "#{node['tomcat']['user']}"
end

directory tomcat_base do
        owner tomcat_user
        group tomcat_user
        mode "0755"
        action :create
        recursive true
        not_if { File.exist?("#{node['tomcat']['basedir']}") }
end


remote_file "/tmp/tomcat.tar.gz" do
        #source 'http://10.72.6.182/tars/mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz'
        source 'ftp://192.168.33.10/pub/apache-tomcat-7.0.59.tar.gz'
        path "/tmp/tomcat.tar.gz"
        not_if { File.exist?("/tmp/tomcat.tar.gz") }
        end


execute "Extracting source" do
        command <<-EOH
        tar -zxvf /tmp/tomcat.tar.gz -C #{node['tomcat']['basedir']}/
        EOH
        not_if { File.exist?("/MSVOL/apache-tomcat-7.0.59") }
end

#Executing environment  variable

bash "Exporting catalina variable" do
        code <<-EOH
        cp  /etc/profile /etc/profile_orig
        cat >> /etc/profile <<-EOH1
        export CATALINA_BASE=#{node['tomcat']['home']}
        export CATALINA_HOME=#{node['tomcat']['home']}
        export CATALINA_TMPDIR=#{node['tomcat']['home']}/TEMP
        export CLASSPATH=#{node['tomcat']['home']}/bootstrap.jar:#{node['tomcat']['home']}/bin/tomcat-juli.jar
        EOH1
        EOH
        not_if { File.exist?("/etc/profile_orig") }
end