Wednesday, May 11, 2016

How to reset timezone in Linux/Centos server:

There are two way to do this:

1) cp /usr/share/zoneinfo/Singapore /etc/localtime
*2) ln -s /usr/share/zoneinfo/Singapore /etc/localtime
* For creating soft link, please make sure localtime file should not be exist in /etc path folder. You can remove or rename localtime file first
Update Confluence (wiki) page by node.js

Getting started.....

# mkdir  -p /usr/local/wiki_update_folder
Install confluence-api via npm:
# npm install confluence-api

# vi update_wiki_page_git_version.js

var child = require('child_process')
var Confluence = require("confluence-api");
var config = {
    username: "test-user",    // your confluence userid
    password: "test-pass",      // your confluence password
    baseUrl:  "https://<your_wiki_url>/wiki"
};

var confluence = new Confluence(config);
confluence.getContentByPageTitle("SPACE_NAME", "PAGE_TITLE", function(err, data) {
    ver_no = data.results[0].version.number;
    ver_no=ver_no+1 ;
    child.exec("date",function(err,stdout,stderr){
        confluence.putContent("SPACE_NAME","PAGE_ID",ver_no, "PAGE_TITLE",stdout,function(err, data) {});
    });
}); 



e.g:
If you want to display your script output in wiki. 
It can be very useful in case you want to publish any status into confluence. Also it can be automated by cron or any task scheduler commands. 

var child = require('child_process')
var Confluence = require("confluence-api");
var config = {
    username: "testuser",
    password: "testpass",
    baseUrl:  "https://wiki-confluence-url/wiki"
};


var confluence = new Confluence(config);
confluence.getContentByPageTitle("WIKI_SPACE_NAME", "PAGE_TITLE", function(err, data) {
    ver_no = data.results[0].version.number;
    ver_no=ver_no+1 ;
    shell_comm="sh test.sh'";
    child.exec(shell_comm,function(err,stdout,stderr){
        confluence.putContent("WIKI_SPACE_NAME","WIKI_PAGE_ID",ver_no, "PAGE_TITLE","<pre>"+stdout+"</pre>",function(err, data) {});
    });
});

Read Recent Logs only

Hi All,

Sometimes, we have a situation to read recent logs from the log file which continuously growing from the current logs; source can be anything like system logs, application logs, database logs, device logs etc etc...

My Problem- Need to read all recent slow queries of mongo (nosql database) logs and notified when it breaches threshold (400ms).

Solution- It can be achieved by using various scripting/programming language like node.js or other scripting language, I opt 'BASH script.


#!/bin/bash

## Please update the value according to your system/environment.
## set variable
logfilepath=/var/log/mongo                                          # please change the path as per the environment
name=mongod.log                                                         # mongo log file name
logrecord=<path_to_capture_current_script_log>        # for recording current script log
MaxLLN_file=<file_to_hold_max_LLN_value>         # for maintaing Max Last Line Number
slow_query_log_record=<logged_only_slow_queries>
tmpfile=<temp_file_path>
mailnotification=<notification_filename_which we have to monitored/display/forward>

## Set variable for mail
to='abc@xyz.com'
from='Sender:xxx'
sub='<subject_of_the_mail>'

## No need to change/update anything here. Please change cautiously,if needed.
## If file exists having start line number then save it into a file.
if [ -f $MaxLLN_file ];then
    echo -e "[`date +%F_%T`] $MaxLLN_file File exist, max LLN value is copied" >> $logrecord
    startlineno=`cat $MaxLLN_file`
else
    # If file is not exist
    echo -e "[`date +%F_%T`] $MaxLLN_file doesn't exist so startlineno counter start from 1" >> $logrecord
    startlineno=1
    echo 1 > $MaxLLN_file
fi
while true
do
    startlineno=`cat $MaxLLN_file`
    totallineno=`wc -l $logfilepath/$name|awk '{print $1}'`
    if [ $startlineno -le $totallineno ];then
        echo -e "[`date +%F_%T`] Slow log search start from between $startlineno untill $totallineno line number" >> $logrecord
        slowqeries_count=`sed -n "$startlineno","$totallineno"p $logfilepath/$name | grep -c 'ms'`
        if [ $slowqeries_count -ge 1 ];then
            echo -e "[`date +%F_%T`] $slowqeries_count slow queries found between $startlineno and $totallineno line number" >> $logrecord
            sed -n "$startlineno","$totallineno"p $logfilepath/$name | grep 'ms' > $tmpfile
            `cat $tmpfile >> $slow_query_log_record`
            echo -e "Dear All,\n\nBelow are the Slow query logs of Mongo Server `hostname` ( taking greater than or equal to 400ms)\n\n" > $mailnotification
            cat $tmpfile | while read line
            do
                if [ `echo $line | gawk '{print $NF-400}'` -ge 0 ];then echo $line >> $mailnotification
                fi
            done
            echo -e "\n\n Thanks & Best Regards,\nAnurag Bisht" >> $mailnotification
            echo "Subject: $sub" | cat - $mailnotification | sendmail -F"$from" -t "$to"
            echo > $mailnotification;
        else
            echo -e "[`date +%F_%T`] $slowqeries_count Slow queries found between $startlineno and $totallineno line number" >> $logrecord
        fi
        totallineno=`expr $totallineno + 1`
        echo $totallineno > $MaxLLN_file
        echo -e "[`date +%F_%T`] New search will start from `cat $MaxLLN_file` value" >> $logrecord
        echo -e "---------------------------------" >> $logrecord
    fi
done