Use Python to write a small monitor

Source: Internet
Author: User
1. Getting Started

First of all you have to use a C + +, Java, JavaScript, and so on, programming small white estimate is more difficult, have some programming experience of Python small white relatively simple.

1.1 Hello world!

Python installation is relatively simple, to the official website to download the installation package, the next step is possible. Because 2.6.6 is installed on my server, I also made this version. Say 2.x difference is not very big, if want to use 3.x, perhaps the following code runs directly however, but also is similar, slightly changes can.
Create a new file named hello.py. Using Python's idle open hello.py, write the following code:

Print "Hello world!"

By pressing F5, you can see the results of the output.

1.2 Basic syntax

Each line is a statement. C language is through a semicolon ";" ;
Organize blocks of code by indenting them in. C language is through curly braces "{}";
Note Use the pound sign "#".

1.3 Data types, operators, data structures

The operator is almost the same as the C language, and the C language is basically straightforward to use.
Data types are numeric, string. Data structures are list, tuple, Dict, set. Introduce tuple, cannot be modified, search by index. Dict is similar to map and holds key-value pairs. Take a look at the example to see what tuple uses:

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

1.4 Process Control

You can use if elif else, for, and while in Python for Process control. There are also break and continue. A little different from C, if there is a branch doing nothing, 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 operating result is:
1
AHA 2
AHA 3
AHA 4
AHA 5

1.5 Module organization

There are methods and classes.

Method defines this

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 a static-modified variable in C + +, and all classes are generic; Inheritance is also very simple, so take a look at the article you started recommending.

1.6 Exception Handling

Exception handling is very simple, directly affixed to the code:

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:    # executed after the code block    was run # and all exceptions has been handled, even
  # If a new exception is raised while handling.    Print "We" re-do with that. "

1.7 Engineering Organization

Directly referencing a library, or introducing a method or variable from a library.

Import randomfrom time Import clock

2. Database queries

Since it is monitoring, it is unavoidable to deal with the database. I'm using PostgreSQL, so I'll explain how Python calls Postgres.

Connection Postgres first to install a library psycopg2,windows directly download the installation, pay attention to the choice of version. My server is CentOS, the installation runs directly

Yum Install PYTHON-PSYCOPG2

It's OK.

2.1 First create a database connection

#get database Connectdef Get_con ():    host = ' 127.0.0.1 '  port = ' 5432 '  database = ' Platform '  user = ' Postgr Es '  password = ' Postgres '  conn = Psycopg2.connect (Database=database, User=user, Password=password, Host=host, Port=port)  Return conn

2.2 Execute SQL statement

#执行sql查询def query (conn, SQL):  cursor = conn.cursor ()  cursor.execute (sql)  results = Cursor.fetchall ()  #close cursor  cursor.close ()  return results

2.3 And then you can write a specific business.

Def getusers ():  conn = Get_con () #open connect  sql = "" SELECT * from     t_user     order by Intime DESC     L Imit 5 ""  items = query (conn, SQL)  print str (items)  conn.close () #close Connect

Note that the 3 quotation mark "" "is a normal string, but can be wrapped.

3. Send mail

After querying the data can not notify the Administrator in a timely manner, monitoring is meaningless. So we are notified by email that we can use Python's standard library smtplib directly. Write a function to send the message:

#发送邮件def send_email (Subject, content):  sender = "yourmail@***.com"  password = "******" #密码是看不见的哦  receivers = [tq8117179#163.com] #本人真实邮箱, Welcome to e-mail discussion technical issues  host = "smtp.exmail.qq.com"  port = 465  msg = Mimetext ( Content, ' HTML ', ' Utf-8 ')  msg[' from ' = Sender  msg[' to '] = ",". Join (receivers)  msg[' Subject '] = Header ( Subject, ' utf-8 ')  try:    smtp = Smtplib. Smtp_ssl (host, port)    smtp.login (sender, password)    smtp.sendmail (sender, Receivers, msg.as_string ())  except Exception, E:    logger.error (E)  logger.info (content)

4. Log

We used logger when we sent the mail, how did this logger come about? 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 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 (Filehandler) generates a logger by Logging.getlogger (' Monitor ') and then configures a file processor. Then refer to it in our monitoring program: from log import logger

5. Put the configurable information in the configuration file

What if we add an administrator? What if our email password changes? Directly modify the Python file Ah, haha. Python does not have to be compiled directly to change the code, but our program will be packaged later, so it is best to write a configuration file, Python configuration file reading is very simple, using 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 this:

 #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) r Eturn conn# Send Message 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 ') p ORT = GetConfig (). Getint (' Mail ', ' port ') msg = mimetext (content, ' html ', ' Utf-8 ') msg[' from ' = Sender msg[' to '] = ",". Jo In (receivers) msg[' Subject ' = Header (Subject, ' utf-8 ') try:smtp = Smtplib. Smtp_ssl (host, port) smtp.login (sender, password) smtp.sendmail (sender, Receivers, msg.as_string ()) Except:logg Er.exception ("Exception:") logger.info (content) 

The configuration file is Monitor.ini and reads as follows:

#数据库配置 [db]host = 127.0.0.1port = 5432database = Platformuser = Postgrespassword = postgres# Mail configuration [mail]sender = Yourmail@xxx . Compassword = ****** #多个联系人用英文逗号隔开receivers = Tq8117179#163.comhost = Smtp.exmail.qq.comport = 465

6. Add control

We check the data every 5 minutes, but the business SQL can only query the last few, so add a time limit, get a start, end time.

Start_time = "2015-10-1 16:24:24" end_time = None#update End_time, invoke before get new Datadef update_end_time ():  glo Bal 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_timegetusers can be rewritten as: 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 are not None and Len (items) >0:    count = Len (items)    tip = "and" +str (count) + "users are already registered. "+end_time    send_email (tip, tip+" \ n "+str (items))

Then write a unified schedule:

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 function of the task while, the first update end_time, that is, the current time, after the execution of the start_time updated to just end_time, so there will be no slip through. There is another place to be aware of, the keyword Global. In Python, using global variables is required by the Global keyword to be declared, otherwise there will be a problem.

7. Running

Open the Linux console and run the Python monitor.py directly, but once the shell exits, the task will be stopped. So I chose a process management tool: Supervisor. Supervisor can also be restarted automatically when the process is interrupted.

7.1. Installing Supervisor

Install Python-setuptools First

Yum Install Python-setuptools

Installing Supervisor

Easy_install Supervisor

Generate Supervisor Configuration file

echo_supervisord_conf >/etc/supervisord.conf

Then add in/etc/supervisord.conf:

[Program:monitor]command = Python/usr/monitor/monitor.pydirectory =/usr/monitoruser = root

7.2. Operation Monitoring

Then run Supervisord boot supervisor in the terminal.
Run Supervisorctl in the terminal, enter the shell, run status to view the running state of the script.

7.3. Turn off monitoring and common commands

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

    • Shutdown Stop Supervisor (child process will also be stopped);
    • Start Monitor to start the monitor process service (once the monitor process exits, it will start from);
    • Stop monitor closes the monitor process service;
    • Restart monitor shuts down the running monitor process and restarts the monitor process service;
    • Reload Reload the supervisor configuration file;
    • Exit quit Supervisorctl's shell.

The program is basically finished, can also run up, is not very cool, everyone quickly hands-on practice!

  • 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.