码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-07-16 18:26:54      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:eval   转换   uda   tar   describe   mat   cer   时间   accept   

 

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)

 

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

标签:eval   转换   uda   tar   describe   mat   cer   时间   accept   

原文地址:https://www.cnblogs.com/hxlasky/p/11196327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!