Python 环境:python3

服务器环境: centos6.5

数据库: Mysql

大概流程:在装有Python服务器,利用pymssql库连接MSSQL生产数据库取出数据然后写进mysql数据库里,再从mysql里取出需要的数据,制作对应的报表;

SQLServer 权限:master只读权限,grant view server state to user,或者限定具体系统视图只读权限

贴出部分代码:

后续可以写成类,可以大大提高执行速度,不过此脚本执行频率不高。

###############################   DB_SERVER_SERVICES  ###########################
#!/usr/bin/python
#coding=utf8 import pymssql
import pymysql #Define mssql connections Dic.
servers = {'server':'192.168.10.10','user':'sa','password':'******','database':'master'}
connmysql = pymysql.connect(host='localhost',port=3306,user='root',passwd='******',db='mssql',charset='utf8')
connmssql = pymssql.connect(**servers) curmssql_services = connmssql.cursor()
curmysql_insert = connmysql.cursor() sql01 = """select @@SERVERNAME as servername,servicename,startup_type_desc,status_desc,convert(varchar(100),last_startup_time,23) as last_startup_time,service_account,is_clustered,cluster_nodename
from sys.dm_server_services where servicename like 'SQL Server%' and servicename not like 'SQL Server 代理%'""" curmssql_services.execute(sql01) sql02 = "insert into db_server_services (servername,servicename,startup_type_desc,status_desc,last_startup_time,service_account,is_clustered,cluster_nodename) values(%s,%s,%s,%s,%s,%s,%s,%s)"
curmysql_insert.executemany(sql02,curmssql_services.fetchall())
connmysql.commit() curmssql_services.close()
curmysql_insert.close()
connmssql.close()
connmysql.close() ############################ db_space_percent ##############################
import pymssql
import pymysql #Define mssql connections Dic.
servers = {'server':'10.252.130.198','user':'sa','password':'abc123..','database':'master'}
connmysql = pymysql.connect(host='localhost',port=3306,user='root',passwd='abc123..',db='mssql',charset='utf8')
connmssql = pymssql.connect(**servers) curmssql01 = connmssql.cursor()
curmysql01 = connmysql.cursor() sql01 = """with tt as (
select distinct
@@servername as servername,
vs.volume_mount_point as drive_name ,
cast(vs.total_bytes / 1024.0 / 1024 / 1024 as numeric(18,2)) as total_space_gb ,
cast(vs.available_bytes / 1024.0 / 1024 / 1024 as numeric(18,2)) as free_space_gb,
convert(varchar(100), getdate(), 23)as check_time
from sys.master_files as f
cross apply sys.dm_os_volume_stats(f.database_id, f.file_id) as vs
)
select
servername,
drive_name,
total_space_gb,
total_space_gb-free_space_gb as used_space_gb,
free_space_gb,
cast(cast((total_space_gb-free_space_gb)*100/total_space_gb as numeric(18,2))as varchar(10))+'%' as used_percent,
check_time
from tt
""" curmssql01.execute(sql01) sql02 = "insert into db_space_percent (servername,drivername,total_space_gb,used_space_gb,free_space_gb,used_Percent,check_time) values(%s,%s,%s,%s,%s,%s,%s)"
curmysql01.executemany(sql02,curmssql01.fetchall())
connmysql.commit() curmssql01.close()
curmysql01.close()
connmssql.close()
connmysql.close()

用Python连接SQLServer抓取分析数据、监控 (pymssql)的更多相关文章

  1. 怎么用Python写爬虫抓取网页数据

    机器学习首先面临的一个问题就是准备数据,数据的来源大概有这么几种:公司积累数据,购买,交换,政府机构及企业公开的数据,通过爬虫从网上抓取.本篇介绍怎么写一个爬虫从网上抓取公开的数据. 很多语言都可以写 ...

  2. 数据抓取分析(python + mongodb)

    分享点干货!!! Python数据抓取分析 编程模块:requests,lxml,pymongo,time,BeautifulSoup 首先获取所有产品的分类网址: def step(): try: ...

  3. python数据抓取分析(python + mongodb)

    分享点干货!!! Python数据抓取分析 编程模块:requests,lxml,pymongo,time,BeautifulSoup 首先获取所有产品的分类网址: def step(): try: ...

  4. sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取

    原文:sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取 在多人开发中最头疼的是人少事多没有时间进行codereview,本来功能都没时间写,哪有时间来开会细细来分析代码.软件能跑就行, ...

  5. Python 逆向抓取 APP 数据

    今天继续给大伙分享一下 Python 爬虫的教程,这次主要涉及到的是关于某 APP 的逆向分析并抓取数据,关于 APP 的反爬会麻烦一些,比如 Android 端的代码写完一般会进行打包并混淆加密加固 ...

  6. 【转】Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  7. Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  8. Python 3.6 抓取微博m站数据

    Python 3.6 抓取微博m站数据 2019.05.01 更新内容 containerid 可以通过 "107603" + user_id 组装得到,无需请求个人信息获取: 优 ...

  9. 手把手教你用python打造网易公开课视频下载软件3-对抓取的数据进行处理

    上篇讲到抓取的数据保存到rawhtml变量中,然后通过编码最终保存到html变量当中,那么html变量还会有什么问题吗?当然会有了,例如可能html变量中的保存的抓取的页面源代码可能有些标签没有关闭标 ...

随机推荐

  1. 这家伙好像还不错 ZH奶酪-张贺

    http://www.cnblogs.com/CheeseZH/archive/2012/11/23/2783846.html

  2. Week3_代码复审

    软件工程师的成长 一口气看完了十多篇的博客,心里的感觉五味陈杂.既有对未来道路的憧憬,也有对自己目前水平的无力感,与那些在这个领域打拼十几年甚至几十年的前辈相比,我不过也就是刚刚迈过行业门槛一条腿而已 ...

  3. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

  4. [转帖]SSL/TLS/WTLS原理

    SSL/TLS/WTLS原理 作者:yawl < yawl@nsfocus.com >主页:http://www.nsfocus.com日期:2001-02-19 一 前言 首先要澄清一下 ...

  5. [转帖]devops 容器管理平台 rancher 简介

    https://testerhome.com/topics/10828 chenhengjie123 for PPmoney · 2017年11月13日 · 最后由 c19950809 回复于 201 ...

  6. [From WIKI] IBM Z

    IBM zEnterprise System From Wikipedia, the free encyclopedia     Jump to navigationJump to search Hi ...

  7. poj2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  8. Add Again UVA - 11076(排列之和)

    题意: 输入n个数字,求这些数字 所有全排列的和 (1<= n <= 12) 对于任意一个数字,其在每一位出现的次数是相同的    即所有数字的每一位相加的和是相同的. 因此可以等效为它们 ...

  9. 【题解】 [HNOI2002]营业额统计 (Splay)

    懒得复制,戳我戳我 Solution: \(Splay\)板子题,注意可以选择相等大小 Code: //It is coded by Ning_Mew on 4.10 #include<bits ...

  10. 【Revit API】FamilyInstance、FamilySymbol、Family的寻找关系

    话不多说,做个笔记 FamilyInstance instance; var typeid = instance.GetTypeId(); var symbol = RevitDoc.GetEleme ...