博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Python连接SQLServer抓取分析数据、监控 (pymssql)
阅读量:5323 次
发布时间:2019-06-14

本文共 2899 字,大约阅读时间需要 9 分钟。

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=utf8import  pymssqlimport 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_nodenamefrom 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 pymssqlimport 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_timefrom sys.master_files as fcross apply sys.dm_os_volume_stats(f.database_id, f.file_id) as vs)selectservername,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_timefrom 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()

 

转载于:https://www.cnblogs.com/kingwwz/p/9889899.html

你可能感兴趣的文章
网络丢包严重的解决办法
查看>>
[Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] Array and ArrayList
查看>>
iOS 黑屏
查看>>
linux学习笔记<基本知识普及>
查看>>
Python哈希表的例子:dict、set
查看>>
使用Eclipse构建Maven的SpringMVC项目
查看>>
ajax json 动态传值
查看>>
[Xamarin] 製作Options Menu、Intent 呼叫網址和Market (转帖)
查看>>
bnu 52037 Escape from Ayutthaya
查看>>
C#是类型安全的
查看>>
c++网络编程错误(WSAStartup)
查看>>
在线图床工具的使用 https://sm.ms/
查看>>
MySQL5.7 error log时间显示问题
查看>>
taro 关于Swiper 组件使用 右侧白边问题
查看>>
php实现记住用户名和自动登录
查看>>
Quartz入门例子简介 从入门到菜鸟(二)
查看>>
车辆运动控制算法——MPC
查看>>
函数练习 阶乘累加求和四种方式
查看>>
升级linux bash
查看>>
Angular入门(三) 引入boostrap4
查看>>