首页 > 其他分享 >定时统计vm_stat的一个脚本

定时统计vm_stat的一个脚本

时间:2023-11-03 20:46:08浏览次数:30  
标签:15.2 vmStats 1048576 stat vm f% import 定时 15s%

main.py:

#!/usr/bin/python3
import subprocess
import time
import re
import threading
from vm_stat import VmStat
from start_web_tab import Web

if __name__ == '__main__':
    vm = VmStat()
    vm_stat_th = threading.Thread(target=vm.CollectVmStat)
    vm_stat_th.start()
    web = Web()
    #web_start_th = threading.Thread(target=web.StartChrome())
    #web_start_th = threading.Thread(target=web.StartSafari())

vm_stat.py

#!/usr/bin/python3
import subprocess
import time
import re
import threading

class VmStat(object):
    def __init__(self):
        self.filename = time.strftime('%y-%m-%d %H:%M:%S', time.localtime()) + "_record.txt"

    def CollectVmStat(self):
        file = open(self.filename, "w+")
        self.PrintHeadLine(file)
        file.close()
        for index in range (1, 3000):
            file = open(self.filename, "a")
            self.PrintData(file)
            file.close()
            time.sleep(1)

    def PrintHeadLine(self, file):
        print('MB\n')
        str = "%-20s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s\n" % (
            "time", "active", "inactive", "free", "wired", "swap-in", "swap-out", "page-in", "page-out",
            "stored-comp", "occupied-comp","speculative","throttled","purgeable","page-fault","copy-on-write","zero filled",
            "reactivated","purged","file-back","Anonymous","Decomp","Comp")
        print(str)
        file.write(str)


    def PrintData(self, file):
        timestamp_str = time.strftime('%y-%m-%d_%H:%M:%S', time.localtime())
        vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0].decode()
        vmLines = vm.split('\n')
        sep = re.compile(':[\s]+')
        vmStats = {}
        for row in range(1, len(vmLines) - 1):
            rowText = vmLines[row].strip()
            rowElements = sep.split(rowText)
            vmStats[(rowElements[0])] = int(rowElements[1].strip('\.')) * 16384
        str = "%-20s%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f" \
              "%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f\n" % (
            timestamp_str,
            (vmStats["Pages active"] / 1048576),
            (vmStats["Pages inactive"] / 1048576),
            (vmStats["Pages free"] / 1048576),
            (vmStats["Pages wired down"] / 1048576),
            (vmStats["Swapins"] / 1048576),
            (vmStats["Swapouts"] / 1048576),
            (vmStats["Pageins"] / 1048576),
            (vmStats["Pageouts"] / 1048576),
            (vmStats["Pages stored in compressor"] / 1048576),
            (vmStats["Pages occupied by compressor"] / 1048576),
            (vmStats["Pages speculative"] / 1048576),
            (vmStats["Pages throttled"] / 1048576),
            (vmStats["Pages purgeable"] / 1048576),
            (vmStats["\"Translation faults\""] / 1048576),
            (vmStats["Pages copy-on-write"] / 1048576),
            (vmStats["Pages zero filled"] / 1048576),
            (vmStats["Pages reactivated"] / 1048576),
            (vmStats["Pages purged"] / 1048576),
            (vmStats["File-backed pages"] / 1048576),
            (vmStats["Anonymous pages"] / 1048576),
            (vmStats["Decompressions"] / 1048576),
            (vmStats["Compressions"] / 1048576)
        )
        print(str)
        file.write(str)

start_web_tab.py

#!/usr/bin/python3
import subprocess
import time
import re
import threading

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By


class Web(object):
    def StartChrome(self):
        driver_list = []
        for index in range (1,30):
            driver = webdriver.Chrome()
            #driver.execute_script("window.open('https://www.qq.com/', 'new_window')")  # 打开多个窗口
            driver.get("http://www.qq.com")
            driver_list.append(driver)
            time.sleep(1)
        time.sleep(300)
        for driver in driver_list:
            driver.close()

    def StartSafari(self):
        driver_list = []
        driver = webdriver.Safari()
        for index in range (1,300):
            actionOpenLinkInNewTab = ActionChains(driver);
            actionOpenLinkInNewTab.key_down(Keys.COMMAND).send_keys("t").key_up(Keys.COMMAND).perform();
            #driver.execute_script("window.open('https://www.qq.com/')")  # 打开多个窗口
            driver_list.append(driver)
            time.sleep(1)
        time.sleep(300)
        driver.close()

标签:15.2,vmStats,1048576,stat,vm,f%,import,定时,15s%
From: https://www.cnblogs.com/mooooonlight/p/17808463.html

相关文章

  • C# 判断时间是否在指定时间段内,判断班别时间
    protectedboolgetTimeSpan(stringtimeStr){//判断当前时间是否在工作时间段内string_strWorkingDayAM="07:00";string_strWorkingDayPM="19:00";TimeSpandspWorkingDayAM=DateTime.Parse(_strWorkingDayAM).TimeOfDay;TimeSpands......
  • e1000e 0000:00:1f.6: The NVM Checksum Is Not Valid
    Ubuntu20.04系统,遇到I219网卡不能用的问题,查看dmesg得到如下信息: 解决办法:1.下载Intel官方工具BootUtility: 下载地址:https://www.intel.com/content/www/us/en/download/15755/intel-ethernet-connections-boot-utility-preboot-images-and-efi-drivers.html?下载linu......
  • 第九周Linux教材第五章学习笔记——定时器及时钟服务
    第五章 定时器及时钟服务5.1硬件定时器定时器是硬件设备,包括时钟源和可编程计数器。时钟源通常是一个晶体振荡器,会产生周期性电信号,以精确的频率驱动计数器。使用一个倒计时值对计数器进行编程,每个时钟信号减1。当计数减为0时,计数器向CPU生成一个定时器中断,将计数值重新加......
  • Go语言定时器实战:性能优化与实用技巧
    Go语言定时器实战:性能优化与实用技巧原创 Go先锋 Go先锋 2023-11-0307:58 发表于广东收录于合集#Go语言包29个Go先锋读完需要8分钟速读仅需3分钟  概述在日常开发中,定时器是一个非常常用且重要的功能。它可以让程序在特定的时间间隔内执行某些任务,比......
  • 人工智能算法-SVM, KNN
    目录SVM,KNN区别一、KNN算法概述算法的描述:二、关于K的取值K的取法:三、关于距离的选取EuclideanDistance定义:四、总结SVM,KNN区别SVM:先在训练集上训练一个模型,然后用这个模型直接对测试集进行分类。KNN:没有训练过程,只是将训练数据与训练数据进行距离度量来实现分类......
  • 直播软件搭建,当前时间、既定时间后的时间及时间比较大小
    直播软件搭建,当前时间、既定时间后的时间及时间比较大小获取当前时间 //时间格式SimpleDateFormatdf=newSimpleDateFormat(“yyyy-MM-dd”);//现在的时间Datenow=newDate();//计算某一月份的最大天数Calendarcal=Calendar.getInstance();//Date转化为Calendarc......
  • 预编译语句Prepared Statements,俗称动态sql
    --创建临时表用动态sql创建set@sqls=concat('createtemporarytable',`temptableTableName`,'(variablevarchar(200),variableValuevarchar(200),variableDatevarchar(50))');preparestmt1from@sqls;#准备好sql语句executestmt1;......
  • VMWare虚拟机-修改界面大小(麒麟系统)
    一、下载linux.iso1.下载地址:http://softwareupdate.vmware.com/cds/vmw-desktop/ws/15.5.1/15018445/windows/packages/2.解压后,VMWare添加镜像iso,然后重启虚拟机 3.找到VMwareTools-10.3.10-13959562.tar.gz压缩包并解压         4.执行文件lssudo......
  • VMWARE-虚拟机通外网(麒麟v10系统)
    一、修改网卡1.进入目录下,编辑网卡:cd/etc/sysconfig/network-scriptssudovimifcfg-ens332.配置静态IP、子网掩码、网关、DNS、设备名称:3.重启网卡ifdowneth33--关闭网卡eth33ifupeth33--打开网卡eth33systemctlretsartnetwork二、编辑虚拟网络编辑器1.......
  • JVM 调优实验
    JVM调优理论前言关于性能优化Therealproblemisthatprogrammershavespentfartoomuchtimeworryingaboutefficiencyinthewrongplacesandatthewrongtimes;prematureoptimizationistherootofallevil(oratleastmostofit)inprogramming.—D......