python監控主機磁碟

來源:互聯網
上載者:User

標籤:disk

通過PYTHON實現主機磁碟的使用方式,使用influxdb+grafana來可是互展示

#!/usr/bin/env python

# _*_ coding:utf-8 _*_

import os

import re

import json

import time

from influxdb import InfluxDBClient

def getInfluxdb():

        client=InfluxDBClient(host=‘192.168.15.104‘, port=8086, username=‘admin‘, password=‘root123‘, database=‘mydb‘)

        return client

def check_hd_use():

    cmd_get_hd_use=‘/bin/df‘

    try:

        fp=os.popen(cmd_get_hd_use)

    except:

        ErrorInfo=r‘get_hd_use_error‘

#       print ErrorInfo

        return ErrorInfo

    r_search=re.compile(r‘^/dev/.+\s+(?P<Used>\d+)%\s+(?P<Mount>.+)‘)

    hd_use={}

    for line in fp:

        match=re.search(r_search,line)

        #print ‘......‘,match,

        if match is not None:

            hd_use[match.groupdict()[‘Mount‘]]=match.groupdict()[‘Used‘]

    fp.close()

    #print hd_use

    return hd_use



if __name__==‘__main__‘:

        disk=check_hd_use()

        disk_key=disk.keys()

        disk_key1=disk_key[0]

        disk_key2=disk_key[1]

        disk_value=disk.values()

        disk_value1=disk_value[0]

        disk_value2=disk_value[1]

        current=int(time.time())

        json_body=[{

                "measurement":"disk_use",

                "tags":{"host":"disk_check",

                "time":current

                },

                "fields":{

                disk_key1:int(disk_value1),

                disk_key2:int(disk_value2)

                #‘a‘:disk

                }


             }]

        getInfluxdb().write_points(json_body)

        print json_body

總結:

    需要注意,目前這個代碼擷取的是/,/home的2個磁碟的情況,若有跟多直接添加disk_key,disk_value值                              

本文出自 “DBSpace” 部落格,請務必保留此出處http://dbspace.blog.51cto.com/6873717/1884020

python監控主機磁碟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.