定时统计vm_stat的一个脚本

发布时间 2023-11-03 20:39:44作者: mooooonlight

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()