百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

原来Python可以这样爬虫Cactic监控的巡检项(已附脚本)

toyiye 2024-06-21 12:40 10 浏览 0 评论

前面我的视频(https://www.ixigua.com/i6805866008822153742/,感兴趣的可加关注)讲解了通过编写Python脚本一键化巡检Cacti监控平台监控项的思路,

PPT信息,详细介绍下爬取Cacti监控项的源码



有人评论需要源码的,这不立马写篇文章附上源码:

#! python2.7

# -*- encoding:utf-8 -*-

import os

import urllib2

import cookielib

import urllib

import re

import time

import datetime

import warnings

import csv

from openpyxl import load_workbook

from openpyxl.styles import Font, Alignment, Border, Side

# python2 环境开发

#输入 日期 天气 热点事件

def write_24sr01(read_csv):

#格式化流量

read_list = list(read_csv)

required_col = [4, 2, 3, 5, 6]

results = [0] * 5

for each_line in read_list[10:]:

for i in range(0, 5):

if len(each_line):

x = float(each_line[required_col[i]])

if x > results[i]:

results[i] = x

for i in range(0, 5):

results[i] = round(results[i]/100, 4)

cell = ws.cell(row=5, column=i * 2 + 2) #存储相应的值到excel表中。

cell.value = results[i]

cell.font = font1

cell.alignment = align

cell.number_format = num_format

cell.border = border

def write_24sr02(read_csv):

read_list = list(read_csv)

required_col = 5 #原数据增加了4列导致数据不全 (10列数据取第6列)

result = 0

for each_line in read_list[10:]:

if len(each_line):

x = float(each_line[required_col])

if x > result:

result = x

result = round(result / 1000000000, 2)

cell = ws.cell(row=5, column=11, value=result)

cell.font = font1

cell.alignment = align

cell.border = border

def write_21sr01(read_csv):

# print(read_csv)

read_list = list(read_csv)

# print(read_list)

required_col = 10 #csv的第10列数值是我们需要的流量,流量对比出峰值。

result = 0

for each_line in read_list[10:]:

if len(each_line):

x = float(each_line[required_col])

if x > result:

result = x

result = round(result/1000000000, 2)

# print(result)

cell = ws.cell(row=5, column=12, value=result)

cell.font = font1

cell.alignment = align

cell.border = border

def write_cacti(read_csv,tag,fenmu):

read_list = list(read_csv)

required_col = tag #tag是列数

results = 0

# results = [0] * 5 #得到列表[0, 0, 0, 0, 0] 得到5行数据

for each_line in read_list[10:]: #从第10行开始

if len(each_line):

x = float(each_line[required_col])

if x > results:

results = x

results = round(results / fenmu, 2)

return results

if __name__ == '__main__':

warnings.filterwarnings('ignore')

today = datetime.datetime.now()

yesterday = today + datetime.timedelta(days=-1)

yesterday_str = str(yesterday.strftime("%Y-%m-%d"))

# 输入 日期 天气 热点事件

print u"请输入日期,如 2017-01-01"

print u"或者直接回车使用昨天日期" + yesterday_str

query_day_list = []

while len(query_day_list) == 0:

input_day = raw_input(":")

if input_day == '':

query_day_list = [yesterday_str]

print u'使用默认日期:' + yesterday_str

else:

query_day_list = re.findall(r'(2[0-9]{3}-[0-1][0-9]-[0-3][0-9])', input_day)

# 判断日期格式

if len(query_day_list) == 0:

print u'格式错误'

else:

try: #其实是巡检的前一天的

date_check = datetime.datetime.strptime(query_day_list[0], "%Y-%m-%d")

# 判断日期合法

except Exception as e:

print u'日期错误'

query_day_list = []

query_day = query_day_list[0]

print u"请输入当天巡检员"

name = raw_input(":")

print u"请输入当天天气"

weather = raw_input(":")

print u"请输入当天热点事件"

hot_thing = raw_input(":")

query_day = yesterday_str

cookie = cookielib.CookieJar()

handler = urllib2.HTTPCookieProcessor(cookie)

post_opener = urllib2.build_opener(handler) #获得句柄实例

postUrl = 'http://ip/index.php' #IP根据你Cacti 网址更新

username = 'username'

password = 'password'

postData = {

'action': 'login',

'login_password': password,

'login_username': username,

}

headers = {

'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',

'Accept-Encoding': 'gzip, deflate',

'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US',

'Connection': 'keep-alive',

'Host': 'ip',

'Upgrade-Insecure-Requests': '1',

'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'

}

data = urllib.urlencode(postData)

request0 = urllib2.Request('http://ip/') #IP根据你Cacti 网址更新

response0 = post_opener.open(request0)

request2 = urllib2.Request(postUrl, data, headers)

response2 = post_opener.open(request2)

# 登录CartiEZ成功

start_time = time.strptime(query_day, "%Y-%m-%d")

start_time_float = time.mktime(start_time)

star_time_int = int(start_time_float)

end_time_int = star_time_int + (24 * 60 * 60 - 60)

start_time_str = str(star_time_int)

end_time_str = str(end_time_int)

graph_codes = [ #graph_codes通过Cacti的网址分析得出来的,每次点击流量是通过Get方式获取到相应code的图表。

'1134', '3461', '3129',

'3159', '3351', '2679','4145','4146',

'4070', '4068','3313', '3314', #防火墙

'2299', '2847', '2842', '2843',

'2844', '2845', '2264', '2269',

# '215' 监视器直接拿到源码,对源码的red.gif 还是green.gif 进行判断

]

host_url = 'http://ip/graph_xport.php' # 是分析html源码或者浏览器开发者模式抓包得来。

hostIp = 'file_server' #将生产的excel结果保存到远端文件服务器中

sharePath = r'\\巡检目录\\isp_xunjian'.decode('utf-8')

filename = r'巡检记录-2020.xlsx'.decode('utf-8')

srcFilename = r'\\' + hostIp + sharePath + '\\' + filename

#获取文档r表示不需要转义

# print srcFilename.encode('utf-8') #ces

today = datetime.datetime.now()

yesterday = today + datetime.timedelta(days=-1)

yesterday_str = str(yesterday.strftime("%Y-%m-%d"))

dir_path = r'\\' + hostIp + sharePath + '\\' + yesterday_str

if not os.path.exists(dir_path):

os.makedirs(dir_path)

wb = load_workbook(srcFilename)

ws = wb.active # 不用sheet = wb['Shett1']

font1 = Font(size=9, )

align = Alignment(horizontal='center')

num_format = '0.00%'

side = Side(style='thin', color="000000")

border = Border(left=side, right=side, top=side, bottom=side)

rows = ws.max_row

cols = ws.max_column

col_null = 0

result = []

for i in range(0, 20):

get_param = {

'graph_end': end_time_str,

'graph_start': start_time_str,

'local_graph_id': graph_codes[i],

'rra_id': '0',

'view_type': 'tree'

}

get_url = urllib.urlencode(get_param)

full_url = host_url + '?' + get_url

# print (full_url) # 需要测试用

response = post_opener.open(full_url)

csv_file = csv.reader(response)

if graph_codes[i] == '1134':

result.append(write_cacti(csv_file,3,1000)) #这里注意CSV处理列从0开始的 #csv对应作为参数

elif graph_codes[i] == '3461' or graph_codes[i] =='3129' :

result.append(write_cacti(csv_file, 2,1000000000)) # 第二个参数2表示第三列值,第三个参数表示流量以G为单位

elif graph_codes[i] == '3159':

result.append(write_cacti(csv_file, 1, 1))

elif graph_codes[i] == '3351' or graph_codes[i] == '2679' or graph_codes[i] == '4145' or graph_codes[i] == '4146' or graph_codes[i] == '4070' or graph_codes[i] == '4068' \

or graph_codes[i] == '3313' or graph_codes[i] == '3314' :

result.append(write_cacti(csv_file, 2, 1000000000))

elif graph_codes[i] == '2299' or graph_codes[i] == '2847':

result.append(write_cacti(csv_file, 25, 1000000000))

elif graph_codes[i] == '2842' or graph_codes[i] == '2843' or graph_codes[i] == '2844' or graph_codes[i] == '2845' \

or graph_codes[i] == '2264' or graph_codes[i] == '2269':

result.append(write_cacti(csv_file, 1, 1000000000))

for m in range(2,12): #格式化Excel单元格

cell = ws.cell(row=m, column=cols+1)

cell.font = font1

cell.alignment = align

cell.border = border

ws.cell(row=2, column=cols+1).value = time.strftime("%Y-%m-%d", time.localtime()) #日期

ws.cell(row=3, column=cols+1).value = name

ws.cell(row=4, column=cols+1).value = weather

ws.cell(row=5, column=cols+1).value = hot_thing

ws.cell(row=6, column=cols+1).value = result[0]

ws.cell(row=7, column=cols+1).value = u'24-SR:' + str(result[1]) + u'*4' + '\n' + u'21-SR:' + str(result[2]) + u'*2'

ws.cell(row=8, column=cols+1).value = result[3]

ws.cell(row=9, column=cols+1).value = u'山石:' + str(result[4]+ result[5]) + u'\n' + u'华为:' + str((result[6]+ result[7])/2) + u'\n' + u'郊县-01:' + str(result[8]+ result[9]) + u'\n' + u'郊县-02:' + str(result[10]+ result[11])

ws.cell(row=10, column=cols+1).value = u'IN-MAX:' + str(result[12]) + u'\n' + u'OUT-MAX:' + str(result[13])

ws.cell(row=11, column=cols+1).value = u'XH-ae29:' + str(result[14] + result[15]) + u'\n' + u'BJ-ae27:' + str(result[16] + result[17]) + u'\n' + u'SD-ae8:' + str(result[18] + result[19])

#view-source:http://ip/graph_xport.php?local_graph_id=3540&rra_id=0&view_type=tree&graph_start=1534055781&graph_end=1534660581

#网页元素或源码分析可获取到如上关键链接,Cacti每5分钟采集一次的数据存储在csv文件里。我们需要读取csv的相应数据。

pic_names = [

r'\拨号并发数.png', r'\SR流量监控-1.png', #导出你需要的流量图

]

host_url = 'http://ip/graph_image.php'

for i in range(0, 20):

get_param = {

'graph_end': end_time_str,

'graph_start': start_time_str,

'local_graph_id': graph_codes[i],

'rra_id': '0',

'view_type': 'tree'

}

get_url = urllib.urlencode(get_param)

full_url = host_url + '?' + get_url

response = post_opener.open(full_url)

pic = response.read()

f = open(dir_path + pic_names[i].decode('utf-8'), 'wb')

f.writelines(pic)

f.close()

print '生成'.decode('utf-8') + dir_path + pic_names[i].decode('utf-8')

time.sleep(5)

#需要

try:

wb.save(srcFilename)

except Exception as e:

print str(e)

对应代码这里提出如下问题,供大家思考动动手:

  1. Cacti监控项的主机状态提取出来。
  2. zabbix的怎么取值,给个范例。(后面一节我会公布自己的脚本)

网络方面感兴趣或者对Python感兴趣的同学可以持续关注我,我会不定时发布工作中遇到的编程案例或者网络故障案例。一起交流学习。

相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码