Sunday 18 November 2018

Nagios Monitoring

Nagios Core

Nagios is an open source monitoring system for computer systems. It was designed to run on the Linux operating system and can monitor devices running Linux, Windows and Unix operating systems (OSes).

Basically this utility runs periodically to check critical services which are running on different platforms. For example, Nagios can monitor memory usage, disk usage, currently running processes, check if any process consumed huge amount of memory etc. It can also monitory SMTP, POP, HTTP and some other common network protocol.

Nagios come up with command line interface as well as graphical user interface. But many people uses CLI as it is bit easier to operate and manage things quickly. 
There is good plugin i.e. NRPE which allows you to remotely execute Nagios plugin on other Linux/Unix machine. You needs to have ssh connection between Nagios server and it's clients. You can execute scripts and check metrics on remote windows machine as well.


Installation of Nagios Core (4.1.1)

Pre-requisite: 

1) Needs to have CentOS Linux release 7.2.1511 (Core) 64 Bit O.S
2) yum install -y httpd php
3) yum install -y gcc glibc glibc-common make gd gd-devel net-snmp
4) yum install perl perl-devel
5) yum install unzip
6) yum install wget
7) yum install openssl-devel
8) Needs to have one user `useradd nagios`
9) Needs to have one group `groupadd nagcmd`
10) Run command `usermod -G nagcmd nagios`
11) Run command `usermod -G nagcmd apache`


11) Create directory /root/nagios
12) Untar above downloaded files..
13) cd /root/nagios/nagios-4.1.1
14) Run command: ./configure -with-command-group=nagcmd

15) Run command: make all
16) Run command: make install

Now we need to install init scripts, the required files to run Nagios from the command line and the sample configuration files with the following command.

17) Run commands: make install-init
                                 make install-commandmode
                                 make install-config

After installation you will find all Nagios object copied under the following location 
"/usr/local/nagios/etc/objects"

This is how we have installed Nagios utility to use via CLI, now we need install it's web component by running the following command..
`make install-webconf`

And we need to setup password for the user "nagiosadmin". This username will be used to access interface. So it's important to remember the password that you will type here. Set the password and run the following command and enter the password twice..



It's time to install Nagios plugins by running following command...

1) cd /nagios/nagios-plugins-2.0.3
2) Run command: `./configure --with-nagios-user=nagios --with-nagios-group=nagios
3) make
4) make install

5) Next we have to start Nagios at boot time, so first verify that the configuration file has no errors by running following command
`/usr/local/nagios/bin/nagios -v /usr/local/nagio/etc/nagios.cfg

6) If everything is fine then add the service to run at boot time with this command..
`chkconfig --add nagios`
`chkconfig --level 35 nagios on`
And start the service with following command..
`systemctl start nagios.service`

Installation of Nagios NRPE


Now you can access web portal of Nagios core with beautiful interface
This is all about installation of Nagios utility.  

Now we will go over the various components of Nagios core. 

Modules are like,
1) Hosts
2) Services
3) Contacts
4) Timeperiods
5) Templates
6) Command
7) Nagios Master

Following is the basic diagram of Nagios work flow where in three important modules are being used..
--> Nagios.cfg
--> Localhost.cfg
--> Commands.cfg & Plugins


Going through the above representation...
Nagios.cfg: This is the place where we usually mention the hosts file name for which Nagios is going to monitor..
Once Nagios understand the specific host needs to be monitor then it start looking for that particular host and check configuration which is usually resides under localhos.cfg ( If you have some remote host let say sip123 then you will have file name as sip123.cfg)
Once host found then it start looking for the services which is up and running on that particular host. 
When Nagios check for that particular service then it pass that service name into the command which is usually run from command.cfg file.
That command contains plugin name e.g. check_by_ssh, host name, IP address of that particular host , service name ( service name means the script which resides on the remote host to check specific service) and timeout (in case ssh takes time to connect to that particular remote host)
Once the command run from Nagios server, it will return back with the appropriate result along with status and description of that particular service.

Now let's go over the configuration files..

--> Nagios.cfg 

# You can specify individual object config files as shown below:
cfg_file=@sysconfdir@/objects/commands.cfg
cfg_file=@sysconfdir@/objects/contacts.cfg
cfg_file=@sysconfdir@/objects/timeperiods.cfg
cfg_file=@sysconfdir@/objects/templates.cfg

# Definitions for monitoring the local (Linux) host
cfg_file=@sysconfdir@/objects/localhost.cfg

# Definitions for monitoring a Windows machine
#cfg_file=@sysconfdir@/objects/windows.cfg

# Definitions for monitoring a router/switch
#cfg_file=@sysconfdir@/objects/switch.cfg

# Definitions for monitoring a network printer
#cfg_file=@sysconfdir@/objects/printer.cfg

--> localhost.cfg

define host{
        
use                    linux-server 
host_name         localhost
alias                  localhost
address             127.0.0.1
}


define service{
        
use                             local-service         

host_name                  localhost
service_description      Root Partition

check_command  check_local_disk!20%!10%!/

}
--> commands.cfg

# 'check_local_disk' command definition
define command{

command_name    check_local_disk
command_line      $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$

command_name    check_web_server
command_line       $USER1$/check_by_ssh -H $HOSTADDRESS$ -l op5mon -i                                /opt/monitor/.ssh/id_rsa_check_by_ssh -C                                                     "/usr/lib64/nagios/plugins/check_web_server.sh -E -t 30

}

Now this check_web_server.sh file is resides on your remote server where this service is being monitored. 
One needs to write service as per the requirement. When we write service then we need to write in Nagios format having specific return code as below...
Let say I want to monitor service for web server whether it is up and running or not.

--------------------------------------------------------------

#!/bin/ksh 
#
# Nagios plugin for checking tomcat running 
#
ARC_NAGIOS_RETURN_OK=0
ARC_NAGIOS_RETURN_WARNING=1
ARC_NAGIOS_RETURN_CRITICAL=2

sysName=`uname -n`

arc_check_for_tomcatRunning(){

   arc_rc=$ARC_NAGIOS_RETURN_OK
   arc_reason="Tomcat is running on $sysName."

   #Check to make sure Tomcat is running
   tel=`ps -ef |grep tomcat |grep -v "grep" |wc -l`
   if [ $tel -eq 0 ]
   then
arc_rc=$ARC_NAGIOS_RETURN_CRITICAL
arc_reason="Tomcat is not running on $sysName."
   fi

   echo $arc_reason
   return $arc_rc

   #End checking for tomcat running
}

arc_check_for_tomcatRunning
exit $?
-------------------------------
-------------------------------

Few supporting files are below..

--> template.cfg

This file come up with Nagios utility by default. This contains values which is going to apply while running the Nagios checks...this file is common to all hosts and services you configure in Nagios.
One can have customize configuration in localhost.cfg file itself and rest of the values take from template.cfg file.

define service{
        
name                                        generic-service
active_checks_enabled              1       
passive_checks_enabled            1   
parallelize_check                       1       
obsess_over_service                 1       
check_freshness                       0       
notifications_enabled                1       
event_handler_enabled             1       
flap_detection_enabled             1       
process_perf_data                    1        
retain_status_information         1       
retain_nonstatus_information   1       
is_volatile                               0       
check_period                          24x7
max_check_attempts              3
normal_check_interval           10
retry_check_interval               2
contact_groups                      admins
notification_options     w,u,c,r
notification_interval               60
notification_period                 24x7
register                                 0       
}

--> contacts.cfg

This config file provides you with some example contact and contact group definitions that you can reference in host and service definitions as well as in template.cfg file.

===============================
Contact
===============================

define contact(){

                    contact_name          nagioadmin
                    use                         generic-contact
                    alias                        Nagios admin
                    email                      nagios_user@localhost
}

===============================
Contact Group
===============================

define contactgroup(){

                    contact_name          admins
                    alias                        Nagios Administrators
                    member                       nagiosadmin
}

--> timeperiod.cfg

You can configure time period to run particular service as per your need.. It's like crontab in Linux/Unix and task scheduler in Windows machine. 
You can reference in host, service, contact, template and dependency definitions.

# 'workhours' 0 definition
define timeperiod{
timeperiod_name workhours
alias                 Normal Work Hours
monday         09:00-17:00
tuesday         09:00-17:00
wednesday         09:00-17:00
thursday                 09:00-17:00
friday         09:00-17:00
}               
 Likewise you can configure timeperiod as per your requirement. Please google it for more details about this feature.

After all above appropriate configurations, restart the Nagios service using following command
`service nagios stop`
`service nagios start`

And hit following url...
http://<IP address of server where Nagios is installed>/nagios
Username: nagiosadmin
Password: <You set while installation>

You will see status of all hosts and services on page at one place.



*************************************END****************************************






Tuesday 23 October 2018

SonarQube with Jenkins

How to integrate Sonarqube with Jenkins (Windows 7/8 64 bit)_


Many of us are working on automation testing where we need to write thousand lines of code to automate test cases. while writting code, one has to follow the standard. One has to put write logic which helps in optimizing your code and performance. 

Here is tool SonarQube which explains about how to install/configure it for continuous integration and improving code quality.

Main reason of using this is opensource platform continuous inspection of code quality.It helps in various task and provide report on duplicate code, code standard, unit test, code coverage, complex code etc...

Let's go over setting up SonarQube with Jenkins.


Pre-requisite:

1) One needs to have Jenkins server 2.138.1 installed and service up and running.
2) Download SonarQube from web and unpack the zip file and copy that folder anywhere on your system.
3) Let's run
C:\Users\<username>\Downloads\sonarqube-7.3.zip\sonarqube-7.3\bin\windowsx86-64\StartSonar.bat
4) One needs to have Maven project which is commited to respective repository and integrated with Jenkins.
5) Go to settings.xml file where maven is installed.
For e.g.
C:\Users\<Username>\apache-maven-3.3.9\conf\settings.xml


Add following in above file..


<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://<jenkinsServerIP>:9000
</sonar.host.url>
</properties>
</profile>


6) Add the following dependency in pom.xml file of your respective project.

<build>
<finalName>arcVirtualQueuing</finalName>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.3.0.603</version>
</plugin>
</plugins>
</build> 

Sunday 21 October 2018

Extent Reporting with Selenium

Selenium reporting using Extent Report and TestNG

Many of us working on automation using Selenium Webdriver to reduce the time and cost and achieve higher percentage of test coverage. Along with this, there needs to have advance reporting which gives us details information of particular test run.


So, Extent report is very high in demand and popular which is being used with Selenium. And we all know, TestNG generates HTML report by default which is not more readable and attractive. We have to put lots of efforts to make it attractive. But this has been provided an ability to implement "IReporter an interface to customize TestNG report.


Extent Report API makes our life easy to generate interactive report with simple configuartion.
Considering you have Maven project created..

Step 1 - Extent Report Maven Dependency :

<dependency>      <groupId>com.aventstack</groupId>      <artifactId>extentreports</artifactId>      <version>3.1.5</version> </dependency>


Step 2 - Add extent report classes.


ExtentManager Class:

package utils.ExtentReports; import com.relevantcodes.extentreports.ExtentReports; public class ExtentManager { private static ExtentReports extent; public synchronized static ExtentReports getReporter(){ if(extent == null){ String workingDir = System.getProperty("user.dir"); //Configure HTML report file location extent = new ExtentReports(workingDir+"\\ExtentReports\\ExtentReport.html", true); } return extent; } }

ExtentTestManager Class:

- Here we are going to create extentTestMap will hold the information.

Instance is created by calling getReporter() method from ExtentManager.

- At StartTest() method, an instance of ExtentTest created and put into extentTestMap with current thread id.

At EndTest() method, test ends and ExtentTest instance obtained from extentTestMap via current thread id.

At getTest() method, return ExtentTest instance in extentTestMap by using current thread id.



package utils.ExtentReports; import com.relevantcodes.extentreports.ExtentReports; import com.relevantcodes.extentreports.ExtentTest; import java.util.HashMap; import java.util.Map; public class ExtentTestManager { static Map extentTestMap = new HashMap(); static ExtentReports extent = ExtentManager.getReporter(); public static synchronized ExtentTest getTest() { return (ExtentTest)extentTestMap.get((int) (long) (Thread.currentThread().getId())); } public static synchronized void endTest() { extent.endTest((ExtentTest)extentTestMap.get((int) (long) (Thread.currentThread().getId()))); } public static synchronized ExtentTest startTest(String testName, String desc) { ExtentTest test = extent.startTest(testName, desc); extentTestMap.put((int) (long) (Thread.currentThread().getId()), test); return test; } }


TestListener Class:


package utils.Listeners; import com.relevantcodes.extentreports.LogStatus; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; import tests.BaseTest; import utils.ExtentReports.ExtentManager; import utils.ExtentReports.ExtentTestManager; public class TestListener extends BaseTest implements ITestListener { private static String getTestMethodName(ITestResult iTestResult) { return iTestResult.getMethod().getConstructorOrMethod().getName(); } //Before starting all tests, below method runs. @Override public void onStart(ITestContext iTestContext) { System.out.println("I am in onStart method " + iTestContext.getName()); iTestContext.setAttribute("WebDriver", this.driver); } //After ending all tests, below method runs. @Override public void onFinish(ITestContext iTestContext) { System.out.println("I am in onFinish method " + iTestContext.getName()); //Do tier down operations for extentreports reporting! ExtentTestManager.endTest(); ExtentManager.getReporter().flush(); } @Override public void onTestStart(ITestResult iTestResult) { System.out.println("I am in onTestStart method " + getTestMethodName(iTestResult) + " start"); //Start operation for extentreports. ExtentTestManager.startTest(iTestResult.getMethod().getMethodName(),""); } @Override public void onTestSuccess(ITestResult iTestResult) { System.out.println("I am in onTestSuccess method " + getTestMethodName(iTestResult) + " succeed"); //Extentreports log operation for passed tests. ExtentTestManager.getTest().log(LogStatus.PASS, "Test passed"); } @Override public void onTestFailure(ITestResult iTestResult) { System.out.println("I am in onTestFailure method " + getTestMethodName(iTestResult) + " failed"); //Get driver from BaseTest and assign to local webdriver variable. Object testClass = iTestResult.getInstance(); WebDriver webDriver = ((BaseTest) testClass).getDriver(); //Take base64Screenshot screenshot. String base64Screenshot = "data:image/png;base64,"+((TakesScreenshot)webDriver). getScreenshotAs(OutputType.BASE64); //Extentreports log and screenshot operations for failed tests. ExtentTestManager.getTest().log(LogStatus.FAIL,"Test Failed", ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot)); } @Override public void onTestSkipped(ITestResult iTestResult) { System.out.println("I am in onTestSkipped method "+ getTestMethodName(iTestResult) + " skipped"); //Extentreports log operation for skipped tests. ExtentTestManager.getTest().log(LogStatus.SKIP, "Test Skipped"); } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) { System.out.println("Test failed but it is in defined success ratio " + getTestMethodName(iTestResult)); }

AnnotationTransformer Class:

package utils.Listeners; import org.testng.IAnnotationTransformer; import org.testng.annotations.ITestAnnotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; public class AnnotationTransformer implements IAnnotationTransformer { @Override public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { annotation.setRetryAnalyzer(Retry.class); } } 


Retry Class:

package utils.Listeners; import com.relevantcodes.extentreports.LogStatus; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.testng.IRetryAnalyzer; import org.testng.ITestResult; import tests.BaseTest; import utils.ExtentReports.ExtentTestManager; public class Retry implements IRetryAnalyzer { private int count = 0; private static int maxTry = 1; //Run the failed test 2 times @Override public boolean retry(ITestResult iTestResult) { if (!iTestResult.isSuccess()) { if (count < maxTry) { count++; iTestResult.setStatus(ITestResult.FAILURE); extendReportsFailOperations(iTestResult); return true; } } else { iTestResult.setStatus(ITestResult.SUCCESS); } return false; } public void extendReportsFailOperations (ITestResult iTestResult) { Object testClass = iTestResult.getInstance(); WebDriver webDriver = ((BaseTest) testClass).getDriver(); String base64Screenshot = "data:image/png;base64,"+((TakesScreenshot)webDriver).getScreenshotAs(OutputType.BASE64); ExtentTestManager.getTest().log(LogStatus.FAIL,"Test Failed", ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot)); }


Step 4 - Add description

@Test (priority=1, description="Scenario of providing empty username and password")
public void invliadTest()
{
ExtentTestManager.getTest().setDescription("Scenario of providing empty username and password");


Step 5 - Adding listener to TestNG.xml file

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="N11 Test Suite" > <listeners> <listener class-name="utils.Listeners.TestListener"/> <listener class-name="utils.Listeners.AnnotationTransformer"/> </listeners> <test name="LoginTest"> <classes> <class name="tests.LoginTests"/> </classes> </test> </suite>


Step 6 - BaseTest Class

package tests; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; public class BaseTest { public WebDriver driver; public WebDriverWait wait; public WebDriver getDriver() { return driver; } @BeforeClass public void setup () { driver = new ChromeDriver(); wait = new WebDriverWait(driver,15); driver.manage().window().maximize(); } @AfterClass public void teardown () { driver.quit(); } }


Step 7 - Test Class

package tests; import org.testng.annotations.Test; import pages.HomePage; import pages.LoginPage; import utils.ExtentReports.ExtentTestManager; public class LoginTests extends BaseTest { @Test (priority = 0, description="Scenario of providing empty username and password") public void invalidLoginTest_InvalidUserNameInvalidPassword () throws InterruptedException { //ExtentReports Description ExtentTestManager.getTest().setDescription("Scenario of providing empty username and password"); HomePage homePage = new HomePage(driver,wait); LoginPage loginPage = new LoginPage(driver,wait); homePage.goToN11(); //Go to LoginPage homePage.goToLoginPage(); //Login to N11 loginPage.loginToN11("onur@swtestacademy.com", "11223344"); //*************ASSERTIONS*********************** Thread.sleep(500); loginPage.verifyLoginPassword(("password")); } @Test (priority = 1, description="Scenario of providing empty username and password") public void invalidLoginTest_EmptyUserEmptyPassword () throws InterruptedException { //ExtentReports Description ExtentTestManager.getTest().setDescription("Scenario of providing empty username and password"); //*************PAGE INSTANTIATIONS************* HomePage homePage = new HomePage(driver,wait); LoginPage loginPage = new LoginPage(driver,wait); //*************PAGE METHODS******************** homePage.goToN11(); homePage.goToLoginPage(); loginPage.loginToN11("",""); //*************ASSERTIONS*********************** Thread.sleep(500); loginPage.verifyLoginUserName("username"); loginPage.verifyLoginPassword("password"); } }


After all this, run the test and you will see html file generated under ExtentReport folder as shown below...or under test-output accordingly. When you open the result you will see test result as shown below:





--------------------------------------------------------------------

Select Language