Write a small monitor using Python and write a monitor using Python

Source: Internet
Author: User

Write a small monitor using Python and write a monitor using Python

1. Getting Started

First of all, you have to use a C/C ++, java, Javascript, and so on. Programming is difficult, and python with some programming experience is relatively simple.

1.1 Hello World!

Python installation is relatively simple. You can download the installation package from the official website and proceed to the next step. Because 2.6.6 is installed on my server, I have also installed this version. 2. x is not very different. If you want to use 3.x, the following code may not run directly, but it is similar. Just change it slightly.
Create a new file named hello. py. Use the python IDLE to open hello. py and write the following code:

print "Hello World!"

Press F5 to view the output result.

1.2 Basic syntax

Each row is a statement. The C language uses semicolons (;).
Organizes code blocks by indentation. The C language uses braces "{}";
Note the use of the "#".

1.3 Data Types, operators, and data structures

The operator is similar to the C language, and some of the C language can be used directly.
Data types include numeric and string. Data structures include list, tuple, dict, and set. This document introduces tuple, which cannot be modified and can be searched through indexes. Dict is similar to map and stores key-value pairs. Let's look at the example and use tuple:

>>> t=(1,2,[1,2])>>> t[2][1, 2]

1.4 Process Control

In Python, if elif else, for, and while can be used for process control. There are also break and continue. One thing is different from C. If a branch does not do anything, use pass. For example

list=[0, 1, 2, 3, 4, 5]for item in list:  if item == 1:    print item  elif item in (2, 3, 4, 5):    print "aha " + str(item)  else:    pass

The running result is:
1
Aha 2
Aha 3
Aha 4
Aha 5

1.5 Module Organization

Methods and classes.

Method Definition

def func(var):   some code here

Classes and C ++ are somewhat different.

class MyClass(object):  common = 1  def __init__(self):    self.myvariable = 5  def myfunction(self, arg1, arg2):    return self.myvariable

The common variable is equivalent to the variable modified with static in C ++. All classes are common. inheritance is also very simple. You can refer to the article that was recommended at the beginning.

1.6 Exception Handling

Exception Handling is very simple, and code is directly pasted:

def some_function():  try:    # Division by zero raises an exception    10 / 0  except ZeroDivisionError:    print "Oops, invalid."  else:    # Exception didn't occur, we're good.    pass  finally:    # This is executed after the code block is run    # and all exceptions have been handled, even    # if a new exception is raised while handling.    print "We're done with that."

1.7 engineering organization

Directly reference the database, or introduce a method or variable from the database.

import randomfrom time import clock

2. Database Query

Since it is monitoring, it is inevitable to deal with databases. I am using PostgreSQL, so I will introduce how python calls postgres.

To connect to ipvs, you must first install a library psycopg2. In Windows, download and install it directly. Be sure to select the correct version. My server is CentOS and runs directly after installation.

yum install python-psycopg2

OK.

2.1 first create a database connection

#get database connectdef get_con():    host = '127.0.0.1'  port = "5432"  database = 'platform'  user = 'postgres'  password = 'postgres'  conn = psycopg2.connect(database=database, user=user, password=password, host=host, port=port)  return conn

2.2 Execute SQL statements

# Run the SQL query def query (conn, SQL): cursor = conn. cursor () cursor.exe cute (SQL) results = cursor. fetchall () # close cursor. close () return results

2.3 then you can write the specific business.

def getUsers():  conn = get_con()#open connect  sql = """select *     from t_user     order by intime DESC     limit 5"""  items = query(conn , sql)  print str(items)  conn.close() #close connect

Note that three quotation marks ("") are common strings, but line breaks are allowed.

3. Send an email

Monitoring is meaningless if the administrator cannot be notified immediately after data is queried. Therefore, we can directly use the python standard library smtplib by email. Write an email sending function:

# Send the email def send_email (subject, content): sender = "yourmail @***. com "password =" ******* "# The password is invisible. receivers = [tq8117179 # 163.com] # My real mailbox, welcome to discuss technical issues via email host = "smtp.exmail.qq.com" port = 465 msg = MIMEText (content, 'html', 'utf-8 ') msg ['from'] = sender msg ['to'] = ",". join (receivers) msg ['subobject'] = Header (Subject, 'utf-8') try: smtp = smtplib. SMTP_SSL (host, port) smtp. login (sender, password) smtp. sendmail (sender, receivers, msg. as_string () failed t Exception, e: logger. error (e) logger.info (content)

4. Logs

We used logger when sending emails. How did this logger come from? Create a new log. py with the following code:

# Coding = utf-8import loggingimport logging. handlerslogger = logging. getLogger ('monitor') logger. setLevel (logging. DEBUG) filehandler = logging. handlers. timedRotatingFileHandler ("/mnt/log/monitor/monitor_log", 'midnight ', 1, 7) # Set the file suffix name filehandler. suffix = "% Y % m % d. log "formatter = logging. formatter ('% (asctime) s-% (name) s-% (levelname) s: % (message) s') filehandler. setFormatter (formatter) logger. addHandler (f Ilehandler) generates a logger through logging. getLogger ('monitor') and configures a file processor. Then we can reference it in our monitoring program: from log import logger

5. Put the configuration information in the configuration file.

What if we add an administrator? What if our email password has changed? Directly modify the python file. Haha. Python does not need to compile and directly change the code, but our program will be packaged later, so it is best to write a configuration file. The python configuration file is very easy to read. Use the python library ConfigParser:

config = None#get configdef getConfig():  global config  if config is None:    config = ConfigParser.ConfigParser()    config.read("monitor.ini")  return config

Then use the following code:

# Get database connectdef get_con (): host = getConfig (). get ('db', 'host') port = getConfig (). get ('db', 'Port') database = getConfig (). get ('db', 'database') user = getConfig (). get ('db', 'user') password = getConfig (). get ('db', 'Password') conn = psycopg2.connect (database = database, user = user, password = password, host = host, port = port) return conn # send the email def send_email (subject, content): sender = getConfig (). get ('mail', 'sender') password = getConfig (). get ('mail', 'Password') receivers = getConfig (). get ('mail', 'receivers '). split (",") host = getConfig (). get ('mail', 'host') port = getConfig (). getint ('mail', 'Port') msg = MIMEText (content, 'html', 'utf-8 ') msg ['from'] = sender msg ['to'] = ",". join (receivers) msg ['subobject'] = Header (Subject, 'utf-8') try: smtp = smtplib. SMTP_SSL (host, port) smtp. login (sender, password) smtp. sendmail (sender, receivers, msg. as_string () handle T: logger. exception ("Exception:") logger.info (content)

The configuration file is monitor. ini. The content is as follows:

# Database Configuration [db] host = 127.0.0.1port = 5432 database = platformuser = ?spassword = s # mail configuration [mail] sender = yourmail@XXX.compassword = ***** # multiple contacts in English comma-separated receivers = tq8117179 #163. comhost = smtp. exmail. qq. compute = 465

6. Add some control points

We check the data every five minutes, but the business SQL can only query the last few, so we need to add a time period limit to get a start time and end time.

Start_time = "2015-10-1 16:24:24" end_time = None # update end_time, invoke before get new datadef update_end_time (): global end_time now = time. mktime (datetime. now (). timetuple () end_time = time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime (now) return end_time # update end_time, invoke after get new datadef update_start_time (): global start_time global end_time start_time = end_time return start_timeg EtUsers can be rewritten to: def getUsers (conn ): global start_time global end_time SQL = "select * from t_user where intime> =" + "'" + start_time + "' and intime <" + "'" + end_time + "'; "items = query (conn, SQL) if items is not None and len (items)> 0: count = len (items) tip =" "+ str (count) + "users have already registered. "+ End_time send_email (tip, tip +" \ n "+ str (items ))

Then write a unified scheduling:

def task():  #init end_time and start_time, must init end_time first!!!  end_time = update_end_time()  start_time = update_start_time()  #init config  getConfig()  while True:    conn = get_con()   #open connect    end_time = update_end_time()    ############## process ##############    logger.info("query: "+end_time)    getUsers (conn)    #do some task else here    ## end    update_start_time()    conn.close()#close connect    time.sleep(5*60)  #end of whiledef run_monitor():  monitor = threading.Thread(target=task)  monitor.start()if __name__ == "__main__":  run_monitor()

In the while of the task function, update the end_time, that is, the current time. After the execution, update start_time to the end_time. In this way, no internet leakage occurs. The keyword global is also worth noting. In python, global variables are declared using the global keyword. Otherwise, problems may occur.

7. Run

Open the linux console and run python monitor. py directly. However, once the shell exits, the task stops. So I chose a process management tool: Supervisor. The Supervisor can automatically restart when the process is interrupted.

7.1. Install supervisor

First install python-setuptools

yum install python-setuptools

Install supervisor

 easy_install supervisor

Generate a supervisor configuration file

 echo_supervisord_conf > /etc/supervisord.conf

Then add the following in/etc/supervisord. conf:

[program:monitor]command = python /usr/monitor/monitor.pydirectory = /usr/monitoruser = root

7.2. Operation Monitoring

Then run supervisord on the terminal to start the supervisor.
Run supervisorctl on the terminal, go to shell, run status, and check the running status of the script.

7.3. disable monitoring and Common commands

The following commands are all executed in the shell of supervisorctl.

  • Shutdown to stop the Supervisor (sub-process will also be stopped );
  • Start monitor: Enable the monitor process service. Once the monitor process exits, it starts automatically );
  • Stop monitor: Shut down the monitor process service;
  • Restart monitor: Shut down the running monitor process and restart the monitor process service;
  • Reload: reload the supervisor configuration file;
  • Exit to exit the shell of supervisorctl.

The program is basically finished, and you can run it. Isn't it cool? Please try it out quickly!

Articles you may be interested in:
  • I wrote a Python script to monitor the nginx process.
  • Example of python Dynamic Monitoring log Content
  • Example of using python camera for remote monitoring
  • Example of using python to monitor windows Services and automatically start services
  • Example of python monitoring network card Traffic and drawing using graphite
  • Use Python Supervisor for process monitoring and Automatic startup
  • How to monitor linux performance and process consumption performance using python
  • Example of a service monitoring program written in Python
  • Python script For nic traffic monitoring
  • Using Inotify to monitor file instances in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.