ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Linux Disk 용량 확인 스크립트 with SLACK
    Python 2020. 2. 12. 22:47

    디스크 용량을 확인 하여 리미트 설정에 따른 슬렉 알람 설정

     

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    ##########################################################################
    # NAME          : disk_check.py
    # LAST UPDATE   : 2017-11-16
    # Verersion     : 0.1
    ##########################################################################
    import os
    import logging
    import socket
    import commands
    import string
    import sys
    import fcntl
    import struct
    ### Slack API
    from slackclient import SlackClient
    sc = SlackClient("xoxb-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
    ##########################################################################
    # System info
    host = socket.gethostname()
    def get_ip_address(ifname):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        return socket.inet_ntoa(fcntl.ioctl(
            s.fileno(),
            0x8915,  # SIOCGIFADDR
            struct.pack('256s', ifname[:15])
        )[20:24])
    ip = get_ip_address('eth0')
    ##########################################################################
    # Disk Check
    def getMsg():
            raw_result = commands.getoutput("df -h")
            result = string.split(raw_result, "\n")
    
            for x in result[:] :
                r = x.split()
                msg = ''
                if r[0] == 'Filesystem':
                    continue
                if eval(r[4][:-1]) >= 80 : #Limit check
                  msg = msg + "```### Warning : Disk Space " + r[4] + " ###\n"
                  msg = msg + host + "[" + ip + "]\n"
                  msg = msg + "DISK : " + r[5] + " " + r[4] + " " + r[2] + "/" + r[1] + "```"
                  sc.api_call(
                    "chat.postMessage",
                    channel = "#server_alert",
                    text = msg
                  )
    ##########################################################################
    getMsg()
    sys.exit();

    'Python' 카테고리의 다른 글

    MariaDB 백업 스크립트  (0) 2020.02.12
Copyright 2020. bluewins All Rights Reserved.