Dec 09 2016

Check Service Status With Python on OEL 7 / Centos 7

Category: Linux & Unix,PythonFatih Acar @ 21:42

I write a python code to check mysql service open or close. It is simple and you can change service_name parameter in the code to check another services. You can download from here check_service_status.py Below code maybe can not run because of python language format is tab structure. You can use with download original code check_service_status.py

Check MySQL Service Status


#-*- coding: utf-8 -*-

import os
import commands

service_name = "mysql"

def check_service_status(service_name):

        status = os.system('systemctl status '+service_name+ ' > /dev/null')
        return status

def main():

        if (check_service_status(service_name) == 0):
                print "Running"
        else:
                print "Stopped"
main()

Download

Execute Script
Continue reading “Check Service Status With Python on OEL 7 / Centos 7”

3,130 total views, 6 views today

Tags: Linux Administration, Python, System Administration


Jan 12 2016

Oracle 11g Data Guard Failover With Python Script

Category: Administration,Backup And Recovery,Linux & Unix,PythonFatih Acar @ 10:56

I wrote a python script to perform failover steps and to take product database IP addresses when product database is down. If you dont use data guard broker or other application for failover operations, you can use this script for failover. My scenario is that I have Oracle 11gR2 2 nodes RAC product database and 1 node standby real time apply type data guard database. When the product database is fail, the data guard database can run as product database with take product database IP addresses and service name. Python script will do all steps that take product IP address, take product service name, perform failover steps.

I made test when data guard was archivelog apply type, script could run correctly.

Systems

Product Database : Oracle 11g R2 (11.2.0.3) RAC
Data Guard : Oracle 11g R2 (11.2.0.3) Single Instance With Grid Infrastructure
Operating System : Oracle Linux 6.6
Data Guard Type : Real Time Apply From Redo Logs
Script Name : failover.py

Python script file is failover.py .Firstly, we have to change parameters in script. You can use vi editor. I generally create python files under /opt/scripts directory.

failover_parameters

Script has 3 types take virtual IP process. If you can not see product virtual IP address when you run ifconfig command after run failover.py script, you can use other type virtual IP process.

The product database is fail. We have to run data guard database like product database. We can use failover.py script.

Run failover.py

[root@dg scripts]# python failover.py

runfailover

If all parameters are correct, you can write “evet” and push enter.

Continue reading “Oracle 11g Data Guard Failover With Python Script”

11,041 total views, 4 views today

Tags: Database Administration, Oracle Administration, Oracle Backup and Restore, Oracle Data Guard Failover, Oracle Failover


Feb 28 2013

Python Commands and Functions

Category: PythonFatih Acar @ 16:50

Take value from user.

input(“Enter a number : “)
raw_input(“Enter a character : “)

Counters

range(100) # write from 0 to 100
range(100,200) # write from 100 to 200
range(100,200,2) # write from 100 to 200 as 2 by 2
len(“Fatih”) # character count result 5

File and Directory Operations

listdir(“/home”/) # list all file or directory in /home/ directory

a=os.listdir(“/home/”)
for files in a:
print files

getcwd() # show current directory
chdir(“/var/lib”) # change directory
mkdir(“test”) # create directory
makedirs(“/test/test1/test2”) # create nested directories
rmdir(“test”) # remove directory
removedirs(“/test/test1/test2”) # remove directories
open(“test.txt”,”w”) # create file and delete all exist values and open with write mode
open(“test.txt”,”a”) # open exist file with append mode
open(“test.txt”,”r”) # open exist file with read mode

file_a.open(“test.txt”,”w”)
file_a.write(“Hello World with Python !”)
file_a.close()

file_a.open(“test.txt”,”r”)
file_a.read()
file_a.readlines() # read line by line

remove(“test.txt”) # delete file
rename(“test.txt”,”test1234.txt”) # rename file
str(a) # convert to string
int(a) # convert to integer

28,391 total views, no views today

Tags: Linux Administration, Programming, Python, System Administration


Feb 08 2013

Mail Send With Python

Category: Linux & Unix,PythonFatih Acar @ 11:39

Mail Send Function | File name : sender.py

import string
import smtplib
from email.mime.text import MIMEText
def mailSend(subject,content):

mailserver = “192.168.5.2” # Mail Server IP Address

mail = MIMEText(content)

mail[“From”] = “fromaddress@example.com”

mail[“To”] = “toaddress@example.com”

mail[“Subject”] = subject

gonder = smtplib.SMTP(mailserver)

gonder.sendmail(mailfrom,mailto,mail.as_string())

gonder.quit()

Notification Example for Disk Status Check | File name : diskcheck.py

from sender import *
import os
import commands
def main():

subject = “Nodes Disk Status”

content = “Node 1” + ‘\n\n’

query = “df -h”

query = commands.getoutput(query)

query = query.split(‘\n’)

for answer in query:

content = content + answer + ‘\n’

query = “df -i”

query = commands.getoutput(query)

query = query.split(‘\n’)

content = content + ‘\n’

for answer in query:

content = content + answer+ ‘\n’

query = “ssh node2 df -h”

query = commands.getoutput(query)

query = query.split(‘\n’)

content = content + ‘\n\n’ “Node 2” + ‘\n\n’

for answer in query:

content = content + answer + ‘\n’

query = “ssh node2 df -i”

query = commands.getoutput(query)

query = query.split(‘\n’)

content = content + ‘\n’

for answer in query:

content = content + answer + ‘\n’

mailSend(subject,content)
main()

8,818 total views, no views today

Tags: Python, System Administration


Jan 29 2013

Python Programing Language Structure

Category: PythonFatih Acar @ 20:48

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme, Ruby, Perl, and Tcl. Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts. Using third-party tools, Python code can be packaged into standalone executable programs. Python interpreters are available for many operating systems.

If you are a database or a system admin, you can use python language for any script works. Python allows that writes quickly scripts.

Python-Programming-Language

Basic Python Code Structure, File Name : test.py

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import os #This choosing allows that you can run os module’s functions with writing os.
from os import * #This choosing allows that you can run os module’s functions without writing os.

def test_function():

print “Hello World.. :)”

def main():

try:

test_funtion()

except NameError:

print “Can not write any text”

13,186 total views, no views today

Tags: Python, System Administration