Cacti automatic Drawing Data export Script (ii)

Source: Internet
Author: User

Due to business needs

Previous post link


Cacti graphical data automatic export script (i)


The script written in the previous article can only download the graph of all month data on the 1st of each month

But there are individual graphs that need to be downloaded on a monthly basis

The script has been made minor changes here

Added the judgment of the date when monthly data is downloaded

Requirements are as follows:


1. The script will automatically download daily and monthly data from all graphs in the graph tree every day and every month.

2. The name of the graphic that you add to the drawing tree must be preceded by an * or no addition, but the SQL statement in the script has to be changed

3, the graphics name can not contain spaces and other illegal file name characters in Linux

4, need to do the monthly specified date to export monthly data, you need to specify the graphic name after the parentheses + date such as:

Traffic Export 1 (21)

When the script is downloaded, it automatically determines whether the date and the current date in parentheses are equal. Yes, you can download this graphical monthly data. Otherwise, monthly data will be downloaded on the 1st of each month.

The script content is as follows

# cacti graphical Data Auto-export script #  The script automatically queries all the drawing numbers and graphics names in the graphics tree, uses the wget tool to download,#  add graphics in the graphics tree, and the export system queries the latest list in the database before execution. #  all you have to do is add the graphics you need to export to the graphics tree.   #使用此脚本前应首先更改Cacti图形和数据导出网页的验证模式, after the changes can be free of verification login, there is a certain risk, please be careful! #注释掉graph_image. PHP and graph_xport.php files  [include ("./include/auth.php");] Line # Add [Include ("./include/global.php");]   #使用以下命令添加crontab自动下载列表   What I'm adding here is a daily 00:01 minute download. Restart the Crond service after the add is complete # Here I save the script file in the file folder in the Cacti folder, save the script elsewhere, and add the virtual directory to Apache for file browsing. # echo  "01 00  * * * root  /var/www/html/file/export.sh  > /dev/null 2>&1 " > /etc/cron.d/export #  Shaanxi West PU Data Communication Co., Ltd.   Operation and Maintenance department # by:fenei  2016 January 14 # qq:407603129 email:[email protected]# http:// Babyfenei.blog.51cto.com #!/bin/bashusername= "Root"              #数据库用户名PASSWORD = "passwd"      #数据库密码DBNAME = "Cacti"               #Cacti使用的数据库名称                                 mysql_cmd= "Mysql  -u${username} -p${password}"  rm -rf /tmp/ list.log       #删除旧的下载列表文件 #select replace (title_cache, ' * ', ')   This statement is to remove the * number in the graphics title   All graphics in my graphics tree have an * number   if you do not change this statement to  select title_cache,select_db_sql= "select  Replace (Title_cache, ' * ', '), Graph_tree_items.local_graph_id  from graph_tree_items left  join graph_templates_graph on graph_templates_graph.local_graph_id=graph_tree_items.local _graph_id where graph_tree_items.local_graph_id <> 0 order by  ' ID '  desc "echo ${select_db_sql}  | ${mysql_cmd}  ${dbname}     > /tmp/list.log               #查询图形树表中的图形ID非0的数据并将结果保存至下载列表                                                                                                           #判断是否创建成功if  [ $? -ne 0 ] then echo  "select databases ${dbname} failed&nbsp ..."  >>/var/log/export/ log              # The failure log is added to the log file when the database query fails Fi  cd /var/www/html/file# This command specifies the directory where the exported file is located and can be changed as required. If not specified, it will cause the download to the root directory.   #创建以日期为名称的文件夹mkdir  -p $ (date -d 1 +%y/%m/%d/image/) mkdir -p $ (date  -d 1 +%y/%m/month/image) mkdir -p $ (date -d 1 +%y/%m/%d/data) mkdir  -p $ (date -d 1 +%y/%m/month/data)   #Cacti网址阐述   Here must add '/' on the back   otherwise error url= " http://localhost/"#获取当日日期    judge whether it is number 1th day= ' date +%d '    #下载日流量图  cat /tmp/list.log  | awk  ' nr>1 '  | while read name iddowget  ${url}graph_ Image.php?local_graph_id=${id}&rra_id=1 " -o $ (date -d 1 +%y/%m/%d/image/) ${name}. jpgdone   #下载月流量图cat  /tmp/list.log | awk  ' nr>1 '  | while read  name iddoecho  "$name"  | grep -q  "("     if [  $? -eq 0 ]; then# determine if the download file name contains () day=$ (echo  $name |grep -po  ' (? <=\ () [^ ()]+ (? =\) ') #如果 () is present, then the content within () is assigned to the variable day        if  [  "$DAY"  =  "$day"  ];then                              The date in           #如果day也即 () is the same as the current date. Then download this data         wget  "${url}graph_image.php?local_graph_id=${id} &rra_id=3 " -o $ (date -d 1 +%y/%m/month/image/) ${name}.jpg         fielif  [  "$DAY"  = 01 ];thenwget  "${url}graph_" Image.php?local_graph_id=${id}&rra_id=3 " -o $ (date -d 1 +%y/%m/month/image/) ${ name}.jpgfidone   #下载日流量数据表  cat /tmp/list.log | awk  ' nr>1 '  |  while read name iddowget  "${url}graph_xpOrt.php?local_graph_id=${id}&rra_id=1 " -o $ (date -d 1 +%y/%m/%d/data/) ${name}. xlsdone   #下载月流量数据表cat  /tmp/list.log | awk  ' nr>1 '  | while read  name iddoecho  "$name"  | grep -q  "(" if [ $? -eq 0  ]; then# determine if the download file name contains () day=$ (echo  $name |grep -po  ' (? <=\ () [^ ()]+ (? =\)) ') #如果 () exists, then () Assign values to variables day        if [  "$DAY"  =  "$day"  ]; then# If day is the same as the date in the () and the current date. Then download this data         wget  "${url}graph_xport.php?local_graph_id=${id} &rra_id=3 " -o $ (date -d 1 +%y/%m/month/data/) ${name}.xls         fielif [  "$DAY"  = 01 ];thenwget  "${url}graph_xport.php?" Local_graph_id=${id}&rra_id=3 " -o $ (date -d 1 +%y/%m/month/data/) ${name}.xlsfidoNe 


Specific effects

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7A/3B/wKiom1alzlCi6hPKAAHwGcszlJc516.png "style=" float: none; "title=" 1.png "alt=" Wkiom1alzlci6hpkaahwgcszljc516.png "/>

The script runs the following results

Only monthly data with a specified date are downloaded in the month directory and will be downloaded on the 1st of each month.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7A/3B/wKiom1alzlHAcmIYAAAkp08PX-k818.png "style=" float: none; "title=" 2.png "alt=" Wkiom1alzlhacmiyaaakp08px-k818.png "/>


This article is from the "www.fenei.com" blog, make sure to keep this source http://babyfenei.blog.51cto.com/443861/1738280

Cacti automatic Drawing Data export Script (ii)

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.