Thursday, 29 November 2018

Run command as different user in your shell script

If you have a requirement of running a shell command inside your script without switching to that user, follow this method. This can also be used in a case where 'su - user -c' works on terminal but fails in script.  

Suppose you are logged in as root on your CentOS machine and you want to run your script as root user but in your script there is a particular command which you want to run as different user, please follow this syntax -  

...

sudo - u <username> <command>
...

eg.

sudo - u mahi whoami

Output : mahi





Monday, 26 November 2018

Solution : Yum breaks after python upgrade


 Use this method to make both python 3 & 2 work without breaking yum.  


*   Link your default python path to use python3

# ln -s /usr/bin/python3.6 /usr/bin/python

*   Link python2.x as python2

 # ln -s /usr/bin/python2.7 /usr/bin/python2

*   Edit yum binary  and change the python path to the following -

# vi /usr/bin/yum
#!/usr/bin/python2


Test :

-bash-4.3$ /usr/bin/python -V
Python 3.6.4


-bash-4.3$ /usr/bin/python2 -V
Python 2.7.14


-bash-4.3$ yum update # or anything

Note that the first two you can link as per the default you want to use. Keeping yum separate by changing python path is making sure that it uses python 2x always regardless your default setting.  




 


 




Sunday, 25 November 2018

Python way to query a database and execute a shell command


The following example shows how to connect to a database, query the database and use the result values to perform some operation using python.

In this script, there's a local database which has some employee inventory, We will query some record and send the result as an email notification.


# Including shebang and title 

#!/usr/bin/python
# Python script to query a database and send mail alert using shell command


# Importing modules and Setup mysql connection

# Import Modules

import MySQLdb
import sys
import subprocess

# Setting up DB Connection


connection = MySQLdb.connect (host = "localhost", user = "bingo", passwd = "kurkure", db = "emp_db")


# Cursor Object
cursor = connection.cursor ()

cursor.execute ("select ID,emp_email,manager_email where keyvalue='somevalue';")


# Reading rows and storing to variables 

# Fetching results
data = cursor.fetchall ()
for row in data :
        ID = row[0]
        emp_email = row[1]
        manager_email = row[2]
        print hash,user_email,manager_email;


# Sending email notification by running a shell script

        runn = subprocess.check_call("/<path-to-script>/mail %s %s %s" % (str(row[0]), str(row[1]), str(row[2])), shell=True)

cursor.close ()
connection.close ()

sys.exit()


######## /<path-to-script>/mail script will look like -

'''
echo -e "Hello, \n\n\n Employee notification for - $1) $2  \n\n\n Thanks" | mail -r "do-not-reply@247-inc.net"  -s $"New Record - $2" $3


'''

Sunday, 18 February 2018

Open TSDB


OpenTSDB A Time Series Database


It has TSD (time series daemon) and many command line utility. TSD uses Hbase store and retrieve data. It supports LZO, Snappy, hadoop-lzo compression method.We can communicate with the TSD via telnet protocol, HTTP API, Built in GUI

Installation Requirements -

•HBase 0.92 or later
•GnuPlot 4.2 or later
•Java Development Kit 1.6 or later
•Autotools
•Make
•Python
•Git


H/w Requirements -

•CPU cores: Max (Limit to 50% of your available CPU resources)
•RAM: Min 16 GB
•Disk 1 - OS: 10 GB - Thin provisioned
•Disk 2 - Data: 100 GB - Thin provisioned


Performance & HA


Reliability

•Failure modes :

The TSD becomes unhealthy when HBase itself is down or broken for an extended period of time.
TSD will discard incoming data points when it is unable to reach HBase and unable to flush them to HBase because of the buffer exhaust.
Another case when TSDB unable to keep up with load when Tcollector running on hundreds machines and bombarding TSD.

•Points of Failure of OpenTSDB :
OpenTSDB itself doesn't have any specific SPoF as there is no central component and you can run multiple TSDs on different machines.