博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cpuidle_mediaroom
阅读量:6608 次
发布时间:2019-06-24

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

hot3.png

#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Version 1.0
Created on 2014-3-21
: elen
'''
import re
import sys, os, time
import MySQLdb
#file_path need to be modified as the real path
file_path = '/home/sky/Desktop/python_for_elen/performance_cpuloading/'
file_name = raw_input("Please enter the log name:")
full_path = file_path + file_name
#print full_path
mysql_url = '192.168.20.108'
#mysql_url = 'localhost'
mysql_user = 'root'
mysql_passwd = '123qaz'
#mysql_db = 'python'
mysql_db = 'python'
mysql_table = 'cpu_loading_cpuload_sky'
def idle_cpu():  # 定义tm1函数
    list_idlecpu = []  # 新建空白列表
    list_time = []
    read_log = open(full_path, 'r')  # 赋值变量read_log 读取log文件
    for eachline in read_log:  # 逐行都取log文件
#        print eachline
        # 正则表达式赋值Keyword_date变量
        Keyword_topCPU_wecb = '^([0-9]{2}:?){3}\s+[a-z]+.*'
        # 搜索在eachline逐行读取结果中带Keyword_date的行
        Search_idlecpu = re.search(Keyword_topCPU_wecb, eachline)
        if Search_idlecpu is not None:
            idlecpu = Search_idlecpu.group().split()[-1]
            time = Search_idlecpu.group().split()[0]
            list_idlecpu.append(idlecpu)
            list_time.append(time)  # 将DATE输入空白列表list_date
#    return list_idlecpu
#    print len(list_idlecpu)
#    print len(list_time)
#    return list_time
    con = MySQLdb.Connection(host=mysql_url, user=mysql_user, passwd=mysql_passwd, db=mysql_db);
    print 'Connected to MySQL server'
    cur = con.cursor()
    cur.execute("use %s" % mysql_db)
#    print "debug+++++++++++"
    for i in range(0, len(list_time)):
#        print "INSERT INTO `%s` (`TIME`, `REF_IDLE_CPU`, `IDLE_CPU`) VALUES ('%s','%s','%s') " % (mysql_table, list_time[i], 100, list_idlecpu[i])
        cur.execute("INSERT INTO `%s` (`TIME`, `REF_IDLE_CPU`, `IDLE_CPU`) VALUES ('%s',%s,%s) " % (mysql_table, str(list_time[i]), 100, str(list_idlecpu[i])))
    con.commit()
    # Close MySQL connection
    if con:
        con.close()
try:
    con_0 = MySQLdb.Connection(host=mysql_url, user=mysql_user, passwd=mysql_passwd, db=mysql_db);
    cur_0 = con_0.cursor()
    cur_0.execute("use %s" % mysql_db)
    cur_0.execute("TRUNCATE TABLE `%s`" %mysql_table)
    print "Old data has been removed!!!!"
    if con_0:
        con_0.close()
    time.sleep(10)
    idle_cpu()
    print "Finish!!!!!!!!!!!!"
except Exception, e:
    print e
    exit(1)
    print "Oops, something went wrong..."

转载于:https://my.oschina.net/xxjbs001/blog/191631

你可能感兴趣的文章
Linux Makefile 生成 *.d 依赖文件及 gcc -M -MF -MP 等相关选项说明【转】
查看>>
Linux下安装Python-3.3.2【转】
查看>>
STL杂记
查看>>
LeetCode OJ:Merge Two Sorted Lists(合并两个链表)
查看>>
功能测试
查看>>
Rust的闭包
查看>>
【BZOJ 1901】Dynamic Rankings
查看>>
阿里架构师都在学的知识体系
查看>>
PAT (Advanced Level) 1028. List Sorting (25)
查看>>
【摘】人生苦短, 每日python
查看>>
【转】聚集索引和非聚集索引的区别
查看>>
【转】mac os 安装php
查看>>
Android -- OkHttp的简单使用和封装
查看>>
软件工程_第二次作业
查看>>
C# DllImport的用法
查看>>
ERP框架序设计与开发日记(下)
查看>>
Github-Client(ANDROID)开源之旅(二) ------ 浅析ActionBarSherkLock
查看>>
no identities are available for signing
查看>>
javascript 和 jquery插件开发
查看>>
Linux Shell文件差集
查看>>