Sunday 26 February 2017

Diskspace monitoring script for Sys Admins


#!/bin/sh
df -H |  grep -v 'Filesystem' | awk '{ print $5 " " $1 }' | while read output
do
   usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
   echo $usep
   partition=$(echo $output | awk '{ print $2 }' )
   echo $partition
   if [ $usep -ge 90 ]
   then
echo "sapce has reached the limit"
#send mail utility can be downloaded from google and change the values according to your SMTP server
sendEmail -bcc mail_id_of_team -u Disk Space Status Notification : `hostname` -o message-content-type=html -m "Disk Space Status Notification : `hostname` <br/><br/><table border=\"1\"><tr><td><strong>HOSTNAME</strong></td><td><strong>HEALTH</strong></td><td><strong>FILE SYSTEM</strong></td></tr><tr><td>`hostname`</td><td>$usep%</td><td>$partition</td></tr></tr></table><br/><br/>please take action immediately"

  fi
done

awk command examples for practice for Linux beginners


The basic function of awk is to search files for lines or other text units containing one or more patterns. When a line matches one of the patterns, special actions are performed on that line.

Programs in awk are different from programs in most other languages, because awk programs are
"data-driven": you describe the data you want to work with and then what to do when you find it. Most other languages are "procedural." You have to describe, in great detail, every step the program is to take. When working with procedural languages, it is usually much harder to clearly describe the data your program will process. For this reason, awk programs are often refreshingly easy to read and write.

There are several ways to run awk. If the program is short, it is easiest to run it on the command line:
awk PROGRAM inputfile(s)
If multiple changes have to be made, possibly regularly and on multiple files, it is easier to put the awk
commands in a script. This is read like this:
awk -f PROGRAM-FILE inputfile(s)

Printing selected fields
When awk reads a line of a file, it divides the line in fields based on the specified input field separator, FS
The variables $1, $2, $3, ..., $N hold the values of the first, second, third until the last field of an input line. The variable $0 (zero) holds the value of the entire line.

In the output of ls -l, there are 9 columns. The print statement uses these fields as follows:

ls -l | awk '{ print $5, $9 }'
4096 jenkins_upgrade
57 shellPractice
120 venky.sh
4096 wcpjars

Without formatting, using only the output separator, the output looks rather poor. Inserting a couple of tabs and a string to indicate what output this is will make it look a lot better:
ls -ldh * | grep -v total | awk '{ print "Size is " $5 " bytes for " $9 }'

Size is 4.0K bytes for jenkins_upgrade
Size is 57 bytes for shellPractice
Size is 120 bytes for venky.sh
Size is 4.0K bytes for wcpjars

df -h | sort -rnk 5 | head -3 | awk '{ print "Partition " $6 "\t: " $5 " full!" }'
Partition /boot : 39% full!
Partition /     : 10% full!
Partition /home : 3% full!

\n Newline character
\t Tab

The print command and regular expressions

awk 'EXPRESSION { PROGRAM }' file(s)

df -h | awk '/dev/ { print $6 "\t: " $5 }'
/       : 10%
/dev    : 0%
/dev/shm        : 0%
/home   : 3%
/boot   : 39%

ls -l | awk '/\<(s|u|O).*\.jar$/ { print $9 }'
OnlineDataModel.jar
sharedserviceslib.jar
utilityframework.jar

In order to precede output with comments, use the BEGIN statement:
ls -l | awk 'BEGIN { print "Files found:\n" } /\<[a|x].*\.conf$/ { print $9 }'

The END statement can be added for inserting text after the entire input is processed
ls -l | \
awk '/\<[a|x].*\.conf$/ { print $9 } END { print \
"Can I do anything else for you, mistress?" }'

The field separator is represented by the built-in variable FS.
awk 'BEGIN { FS=":" } { print $1 "\t" $5 }' /etc/passwd
root    root
bin     bin
daemon  daemon
adm     adm
lp      lp

The output from an entire print statement is called an output record. Each print command results in one
output record, and then outputs a string called the output record separator, ORS

awk 'BEGIN { OFS=";" ; ORS="\n-->\n" } { print $1,$2}' test

cat revenues
20021009 20021013 consultancy BigComp 2500
20021015 20021020 training EduComp 2000
20021112 20021123 appdev SmartComp 10000
20021204 20021215 training EduComp 5000

cat total.awk
{ total=total + $5 }
{ print "Send bill for " $5 " dollar to " $4 }
END { print "---------------------------------\nTotal revenue: " total }

awk -f total.awk revenues
Send bill for 2500 dollar to BigComp
Send bill for 2000 dollar to EduComp
Send bill for 10000 dollar to SmartComp
Send bill for 5000 dollar to EduComp
---------------------------------
Total revenue: 19500



sed command examples for practice for Linux beginners

A Stream EDitor is used to perform basic transformations on text read from a file or a pipe

Consider a text file called example which has data like below
This is the first line of an example text.
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.

Printing lines containing a pattern
We want sed to find all the lines containing our search pattern, in this case "erors". We use the p to obtain the result

sed '/erors/p' example
This is the first line of an example text.
It is a text with erors.
It is a text with erors.
Lots of erors.
Lots of erors.
So much erors, all these erors are making me sick.
So much erors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.

As you notice, sed prints the entire file, but the lines containing the search string are printed twice. This is not what we want. In order to only print those lines matching our pattern, use the -n option:

sed -n '/erors/p' example
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.


Deleting lines of input containing a pattern
We use the same example text file. Now we only want to see the lines not containing the search string:

sed '/erors/d' example
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.

Ranges of lines
This time we want to take out the lines containing the errors. In the example these are lines 2 to 4. Specify this range to address, together with the d command:

sed '2,4d' example
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.
The following command prints the first line containing the pattern "a text", up to and including the next line containing the pattern "a line":

sed -n '/a text/,/This/p' example
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
This is a line not containing any errors.

Find and replace with sed
In the example file, we will now search and replace the errors instead of only (de)selecting the lines containing the search string.

sed 's/erors/errors/' example
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.

As you can see, this is not exactly the desired effect: in line 4, only the first occurrence of the search string has been replaced, and there is still an 'eror' left. Use the g command to indicate to sed that it should examine the entire line instead of stopping at the first occurrence of your string:

sed 's/erors/errors/g' example
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these errors are making me sick.
This is a line not containing any errors.
This is the last line.

To insert a string at the beginning of each line of a file, for instance for quoting:

sed 's/^/> /' example
> This is the first line of an example text.
> It is a text with erors.
> Lots of erors.
> So much erors, all these erors are making me sick.
> This is a line not containing any errors.
> This is the last line.

Insert some string at the end of each line:

sed 's/$/EOL/' example
This is the first line of an example text.EOL
It is a text with erors.EOL
Lots of erors.EOL
So much erors, all these erors are making me sick.EOL
This is a line not containing any errors.EOL
This is the last line.EOL

Multiple find and replace commands are separated with individual -e options:

sed -e 's/erors/errors/g' -e 's/last/final/g' example
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these errors are making me sick.
This is a line not containing any errors.
This is the final line.

Just Keep in mind that by default sed prints its results to the standard output, most likely your terminal window. If you want to save the output to a file, redirect it:

sed option 'some/expression' file_to_process > sed_output_in_a_file

if you want to save the formatted text you can use like below

sed -i 's/erors/errors/g' example





Saturday 25 February 2017

Jenkins Interview Questions | Part 1

Q1.  What is Jenkins ?
Ans. It is a continuous integration tool from which we can build, test, deploy application continuously and report status to project team.

Q2. What is Continuous Integration ?
Ans. Continuous Integration is a process where isolated changes are tested frequently and reported at same time. This help us to get quick feedback to Project Team to improve efficiency. 

Q3.  What is the difference between Maven and Jenkins ?

Ans. Maven is Build Automation Technology whereas Jenkins is a continuous integration tool.

Q4.  Which SCM tools Jenkins supports ?

Ans.  Jenkins can support Subversion, Git, Mercurial, Perforce, Clearcase, CVS and RTC
We need to install plugins based on our peoject requirements. In our project we are using git as    SCM tool so we are using git scm plugin in our jenkins.

Q5.  What are the various ways in which build can be scheduled in Jenkins ?

Ans. Builds can be triggered by source code management  commits. ( Poll SCM or Commit Hooks )
        Can be triggered after completion of other builds. (build other projects in post build actions)
        Can be scheduled to run at specified time ( crons )
        Manual Build Requests

Q6.  What is the relation between hudson and Jenkins ?

Ans. Hudson was the earlier name and version of current Jenkins. After some dispute with Oracle , the project name was changed from Hudson to Jenkins.

Q7.  What you do to make sure that your project build doesn't break in Jenkins ?

Ans. I make sure that I perform successful clean install on my local machine with all unit tests.
Then I make sure that I check in all code changes.
Then I do a Synchronize with repository to make sure that all required config and POM changes and any difference is checked into the repository. 

Q7.  What you do when you see a broken build for your project in Jenkins ?

Ans. I will open the console output for the build and will try to see if any file changes were missed.
If not able to find the issue that way, Will clean and update my local workspace to replicate the problem on my local and will try to solve it.

Q8. can you list out some plugins for Jenkins which are used by in your project ?

Ans. 
GreenBalls, Parameterise build plugin, SMTP, Maven Integrator plugin, Build Pipeline plugins, workspace clean plugin, conditional plugin, build pipeline plugin, HTML publisher plugin, dashboard view plugin

Q9. What is Poll SCM in Jenkins ?
Ans. Poll SCM is used to help us in scheduling job when ever there is new commit happened on SCM.
We can configure this setting based on our requirement. In our project we configured for 5 mins.
H/5 * * * *

Q10. How you can notify your project about build status ?
Ans. Jenkins will support multiple types of Notification channel types like Email, Slack and Microsoft team. In our project we are using SMTP plugin to send email about status of jenkins job to our peoject team.

Q11. How do you install Jenkins plugin ?
Ans: There are multiple ways to install jenkins plugin on Jenkins server.
       a)  Go to Manage Jenkins, click on Plugin management option. Under Available tab we can select the plugin which we want to use and click on install.
       b) We can copy jenkins plugin to plugins folder in Jenkins server and restart Jenkins.
       c) Go to Manage Jenkins, click on Plugin management option. Under Advaned tab we can upload plugin directly.

Q12.  How do you conigure proxy settings in Jenkins if Network is proxied in your project ?
Ans.  In our project we don’t have any proxy server configured. Incase if we want to configure, Go to Manage Jenkins, click on Plugin management option. Under Advaned tab we can configurr proxy server

Q13. How do you pass parameters from one job to another job ?
Ans. This can be done from Post build actions. We can choose Trigger Parameterized builds on other project to pass parameters to downstream job.