获取rds的cpu和内存使用情况

 

python版本:2.7.12

复制代码
#!/usr/bin/env python
#coding=utf-8

import time
import os, json, urllib, datetime, shutil
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkrds.request.v20140815.DescribeDBInstancePerformanceRequest import DescribeDBInstancePerformanceRequest

now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days = -1)
key_item="MySQL_MemCpuUsage"
logfilepath="/home/oralce/scripts/monitor_script/logdata/"
instance_list=["1","2","3","4","5","6"]


##获取rds cpu数据并写入文件
def get_cpudata(instance_id,logfile_name):
        client = AcsClient('xxxxx','xxxxx','xxxxx')
        request = DescribeDBInstancePerformanceRequest()
        request.set_accept_format('json')
        start_time = datetime.datetime.strftime(yes_time,'%Y-%m-%d') + 'T16:00Z'
        end_time   = datetime.datetime.strftime(now_time,'%Y-%m-%d') + 'T16:00Z'
        request.set_StartTime(start_time)
        request.set_EndTime(end_time)
        ##获取cpu和内存数据
        request.set_Key(key_item)
        request.set_DBInstanceId(instance_id)
        response = client.do_action_with_exception(request)
        result=json.loads(response)

        list = result['PerformanceKeys']['PerformanceKey'][0]['Values']['PerformanceValue'];
        ##取最新一条数据
        list_length=len(list)
        last_row=list[list_length-1]
        check_utc_date =last_row['Date']

        ##utc时间转换北京时间
        check_date=utc2local(datetime.datetime.strptime(check_utc_date, "%Y-%m-%dT%H:%M:%SZ"))
        check_value = last_row['Value']

        ##数据拼接
        cpu_data=instance_id + '|' + datetime.datetime.strftime(check_date,'%Y-%m-%d %H:%M:%S') + '|' + check_value.replace("&","|")
        
        logfile=logfilepath + logfile_name
        ##写入文件
        with open(logfile, mode='a') as filename:
                filename.write(cpu_data)
                filename.write('\n')


##UTC时间转换成本地时间,参数为datetime类型
def utc2local(utc_st):
    now_stamp = time.time()
    local_time = datetime.datetime.fromtimestamp(now_stamp)
    utc_time = datetime.datetime.utcfromtimestamp(now_stamp)
    offset = local_time - utc_time
    local_st = utc_st + offset
    return local_st


if __name__ == '__main__':
        for instance_id in instance_list:
            ##1实例
            if instance_id =="1" :
                logfile="1_cpu.log"
            ##2实例
            if instance_id =="2" :
                logfile="2_cpu.log"
            ##3实例
            if instance_id =="3" :
                logfile="3_cpu.log"
            ##4实例
            if instance_id =="4" :
                logfile="4_cpu.log"
            ##5实例
            if instance_id =="5" :
                logfile="5_cpu.log"
            ##6实例
            if instance_id =="6" :
                logfile="6_cpu.log"
            
            get_cpudata(instance_id,logfile)
复制代码

 

posted @   slnngk  阅读(905)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示