The previous chapter on the use of Django to build a test site is accessible to a simple monitoring system, the leadership requires that the cloud server CPU memory and other hardware monitoring information, and timely provision of reports
Looked at the open source of this cluster monitoring scheme, decided to first adopt ganglia, after all, simple installation, report rich ~
Install ganglia
The way to compile the installation is very cumbersome, directly using the Yum installation. Cloud Server System redhat6.3
To the Yum configuration file/etc/yum.repo/directory, delete all files in this directory (before deleting, you can rename the file name to Xxx.repo.bak directly)
Then create a new Centos6-base-sohu.repo (directly using the Sohu source, fast), the contents of the file are as follows:
Note The system version, my system for redhat6.3, if it is a 7.x version, BaseURL change to/centos/7/can be
# Centos-base.repo # The Mirror system uses the connecting IP address of the client and the # update s Tatus of each mirror to pick mirrors that are updated to and # geographically close to the client.
You are should use this for CentOS updates # unless to are manually other picking.
# # If The mirrorlist= does not work for your, as a fall back you can try the # remarked out Baseurl= line instead. # [Base] name=centos-6.3-base baseurl=http://mirrors.sohu.com/centos/6/os/x86_64/gpgcheck=0 enabled=1 [ Epel] Name=epel baseurl=http://mirrors.sohu.com/fedora-epel/6/x86_64/gpgcheck=0 enabled=1 [Extra] Name=ext RA baseurl=http://mirrors.sohu.com/centos/6/extras/x86_64 gpgcheck=0 enabled=1 [UPDATE] Name=extra baseurl= http://mirrors.sohu.com/centos/6/updates/x86_64 gpgcheck=0 enabled=1 [OpenStack] Name=cloud baseurl=http://mirrors. Sohu.com/centos/6/cloud/x86_64/openstack-kilo/gpgcheck=0 enabled=0
Note: If it is CentOS system, direct yum install-y epel-release install Epel pack on it
Server-side Installation
Yum install rrdtool Ganglia-gmetad ganglia-gmond ganglia-web httpd php
Client-side Installation
Yum Install Ganglia-gmond
Ganglia Configuration
Server side needs to configure/etc/ganglia/gmond.conf and/etc/ganglia/gmetad.conf two files
> vi/etc/ganglia/gmond.conf using multicast mode, only need to change the following three places
cluster {
name = ' Toltech_launcher '//This name is the same as cluster grouping
...
}
Udp_send_channel {
Mcast_join = 239.2.11.72
port = 8649
ttl = 1
}
udp_recv_channel {
mcast _join = 239.2.11.72
port = 8649
bind = 239.2.11.72
Retry_bind = True
}
Configures the data source for the server side, Toltech_launcher is consistent with the cluster name value in gmond.conf.
Because the server side also installs the gmond as the monitored side, the data source here directly configures the server native localhost, or it can be configured as the other IP on the cluster monitored side. Or configure multiple data sources
> vi/etc/ganglia/gmetad.conf
Data_source "Toltech_launcher" localhost:8649
Distribute server-side/etc/ganglia/gmond.conf files to client side, end of configuration
> scp/etc/ganglia/gmond.conf root@{ip}:/etc/ganglia/gmond.conf
Run Ganglia
Server Side
Service Gmond Start
Service Gmetad Start
Service httpd Start
Client Side
Service Gmond Start
Test
# command line print current active client
Gstat-a
Web Access Http://{your_ip}/ganglia
python crawls the report picture and sends the message
Ganglia has been installed and can see a variety of hardware monitoring reports from the Web. But leaders don't have time to open websites to see so many reports,
How to let the leadership know that we are working hard, of course, the initiative to send the report to the leadership of the mailbox ~
Carefully observe the ganglia of the various reports in the picture address, you can find that each report picture is a regular address
For example, cluster total CPU report:
http://ip/ganglia/graph.php?r=day&z=xlarge&c=toltech_launcher&m=load_one&s=by+name&mc= 2&g=cpu_report
Parameter R=day, represents the last day of monitoring reports, if you want to report on an hour, change to R=hour can be, there are weekly, monthly, etc. can observe the corresponding parameters
C=toltech_launcher, obviously represents the corresponding data source of the report, I represent here is cluster grouping, different application uses different grouping, convenient operation Dimension view
G=cpu_report, represented as CPU reports, other G=network_report network reports, G=mem_report memory reports, and so on.
and monitoring information for each node in the cluster.
Like #集群内各节点剩余内存http://ip/ganglia/stacked.php?m=mem_free&c=toltech_launcher&r=day&st=1514511038. &host_regex=
M=mem_free, indicating that the current is the remaining available memory monitoring reports, can also be set to M=cpu_user, M=disk_free, M=load_one and other monitoring information, specific parameters to see the corresponding report picture URL
Other R=day, c=toltech_launcher with the above cluster total monitoring report parameters consistent
After you figure out what the URL parameter means, just assemble the URL of the monitor report that we want to generate, and then download and send the message.
Using Python, the server is configured to execute this script on a regular basis. The script is simpler, the code is directly attached
#coding: Utf-8 Import urllib,urllib2,socket,re import time,os import smtplib, sys,datetime from Email.mime.text import MIM EText from email.mime.image import mimeimage to Email.mime.multipart import Mimemultipart from Email.header import Hea
Der Reload (SYS) sys.setdefaultencoding ("Utf-8") Downloadpath = ' e:\\2014\\ ' def gethtml (URL): # To set the request header, let the server know that it is not a robot User_agent = ' mozilla/4.0 (compatible; MSIE 5.5; Windows NT) ' headers = {' User-agent ': user_agent} request=urllib2.
Request (url,headers=headers);
page = Urllib2.urlopen (request); html = page.read () return HTML def sendMail (Body,urls): # Third-party SMTP service mail_host= ' smtp.xxx.cn ' #设置服务器 mai L_user= "xxxx@xxxx.cn" #用户名 mail_pass= "xxxx" #口令 sender = ' xxxxx@xxxx.cn ' receivers = [' xxxx@ Qq.com '] # receive mail, can be set to your QQ mailbox or other mailbox #创建一个带附件的实例 message = Mimemultipart () message[' from '] = Header (" XX ", ' utf-8 ') message[' to '] = Header (", ". Join (receivers), ' utf-8 ') today = Datetime.datetime.today ();
Subject = ' XXXXX Cluster monitor Day report ' + today.strftime ('%y-%m-%d%h:%m:%s ') message[' subject '] = Header (subject, ' utf-8 ') #邮件正文内容 Message.attach (Mimetext (body, ' html ', ' Utf-8 ')) #设置邮件名片 (HTML format) # HTML = file (' qianming.html '). Rea
D (). Decode ("Utf-8") # Message.attach (mimetext (HTML, ' HTML ', ' Utf-8 ')) for index, item in enumerate (URLs): Downloadimg (Item,index,downloadpath) #下载报表图片 # binary mode read picture with open (Downloadpath + '%s.jpg '% index , ' RB ') as F:msgimage1 = Mimeimage (F.read ()) # define picture ID Msgimage1.add_header (' Content-id ', '%s '% index] Message.attach (msgImage1) try:smtpobj = Smtplib. SMTP () Smtpobj.connect (Mail_host, 25) # 25 for SMTP port number Smtpobj.login (Mail_user,mail_pass) SMT Pobj.sendmail (sender, Receivers, message.as_string ()) print "Mail sent successfully" except Smtplib.
Smtpexception,e:Print "Error: unable to send mail" + repr (E) deletimg (downloadpath) #下载图片 def downloadimg (url,id, path): Urllib.urlre Trieve (URL, path+ '%s.jpg '% id) #删除下载文件夹内的图片 def deletimg (path): For file in Os.listdir (path): TargetFile = Os.path.join (path, file) if Os.path.isfile (targetfile): Os.remove (targetfile) #图片url urls = [] # Cluster total load urls.insert (0, "http://ip/ganglia/graph.php?r=day&z=xlarge&c=toltech_launcher&m=load_one&s =by+name&mc=2&g=load_report ") #集群内存使用 Urls.insert (1," http://ip/ganglia/graph.php?r=day&z=xlarge& C=toltech_launcher&m=load_one&s=by+name&mc=2&g=mem_report ") #集群CPU Urls.insert (2," http://ip/ Ganglia/graph.php?r=day&z=xlarge&c=toltech_launcher&m=load_one&s=by+name&mc=2&g=cpu_ ") #集群网络IO使用 Urls.insert (3," http://ip/ganglia/graph.php?r=day&z=xlarge&c=toltech_launcher&m= Load_one&s=by+name&mc=2&g=network_report ") #集群内各节点负载概况 Urls.inseRT (4, "http://ip/ganglia/stacked.php?m=load_one&c=toltech_launcher&r=day&st=34533545") #集群内各节点CPU使用 Urls.insert (5, "http://ip/ganglia/stacked.php?m=cpu_user&c=toltech_launcher&r=day&st=3454532& host_regex= ") #集群内各节点剩余内存 Urls.insert (6," http://ip/ganglia/stacked.php?m=mem_free&c=toltech_launcher&r= day&st=3454532&host_regex= ") #集群内各节点剩余硬盘容量 Urls.insert (7," http://ip/ganglia/stacked.php?m=disk_free&
C=toltech_launcher&r=day&st=3453453&host_regex= ") today = Datetime.datetime.today (); BODY = "" "<H2>XXX server cluster daily monitoring report%s (auto send)
Message effects