Script case for use (3)

Hello everyone, my name is Jack (pigeon).

I shared with you " 7 Very Practical Shell Script Examples!" " and " Super Hardcore! 11 Useful Python and Shell Scripting Examples! " two articles.

I got a message from many readers and friends saying: It's so practical.

In my recent work, I have encountered several actual scenarios and used scripts, including: Zabbix automatically discovers monitoring URLs, Alibaba Cloud SMS interface, and deletes Elasticsearch indexes X months ago every week. The details are as follows:

1. Zabbix automatically discovers monitoring URLs

This script is used to monitor URLs, automatically discover files or other stored URLs.

1.1 URL auto-discovery script

from os.path import abspath, dirname, join
import json
import sys


reload(sys)
sys.setdefaultencoding('utf-8')

# URL存储文件
# 每一行格式为(可写多行):应用名称 URL地址 注释
URL_FILE_PATH = join(dirname(abspath(__file__)), 'web_url_list')

# 输出结果,zabbix官方定义的格式
data = {'data': []}

with open(URL_FILE_PATH, 'r') as read_f:
    # 读取URL文件,并进行分割,组成官网定义数据
    for line in read_f:
        if line:
           name = line.split()[0]
           url = line.split()[1]
           description = line.split()[2]
           data['data'].append({"{#NAME}": name, "{#URL}": url, "{#DESCRIPTION}": description})

print json.dumps(data)

1.2 URL Test Script

from sys import argv
import requests

# 获取URL状态
def get_url(url, timeout=5):
    # 多数情况下返回200即为访问成功,提前测试好自己的URL访问返回状态码
    try:
        request_obj = requests.get(url, timeout=timeout)
        status_code = request_obj.status_code
    except Exception as e:
        status_code = 99999

    print(status_code)


url = argv[1]
get_url(url)

1.3 zabbix configure custom key

UserParameter=discovery_web_url, /bin/python /opt/zabbix_scripts/web_url/url_discovery.py 
UserParameter=url_get[*], /bin/python /opt/zabbix_scripts/web_url/url_get_status.py $1

1.4 zabbix WEB configuration

  • Configure Autodiscover

insert image description here

  • Add monitoring items, trigger prototypes

insert image description here
insert image description here

2. Alibaba Cloud SMS Interface

Configure SMS sending according to the official website example

2.1 Alibaba Cloud Configuration

  • Signature configuration: Console - SMS Service - Domestic Message - Signature Configuration

insert image description here

  • Template configuration: console - SMS service - domestic news - template configuration

insert image description here

Define the variables yourself. If the alarm message is too long (35 characters by default), submit a work order to remove the restriction.

  • AccessKey Configuration: Avatar-AccessKey Management

insert image description here

2.2 Send script

#coding=utf-8

   from aliyunsdkcore.client import AcsClient
   from aliyunsdkcore.request import CommonRequest
   from sys import argv


   LOG_FILE_PATH = '/tmp/zabbix-sms.log'


   def sendSms(phone_numbers, subject, alert_message):
       # AccessKet
       client = AcsClient('AccessKey ID', 'Secret')
       alert_message = subject + alert_message

       # 官方定义的格式,具体含义没有研究
       request = CommonRequest()
       request.set_accept_format('json')
       request.set_domain('dysmsapi.aliyuncs.com')
       request.set_method('POST')
       request.set_protocol_type('https') # https | http
       request.set_version('2017-05-25')
       request.set_action_name('SendSms')

       request.add_query_param('PhoneNumbers', phone_numbers)
       # 签名名称
       request.add_query_param('SignName', "XXXX")
       # 模板code
       request.add_query_param('TemplateCode', "XXXX")
       # 传递的参数
       request.add_query_param('TemplateParam', "{\"alert_message\":\"%s\"}" % alert_message)

       response = client.do_action(request)
       return response

   def writeLog(message, response, log_file_path):
       with open(log_file_path, 'a') as a_file:
           a_file.write(message + ' | ' + response)

   if __name__ == '__main__':
       phone_numbers = argv[1]
       subject = argv[2]
       alert_message = argv[3]
       # 手机号、主题、告警信息
       # 由zabbix端传递过来的
       res = sendSms('130XXXXXXXX', subject, alert_message)
       writeLog(subject + alert_message, res, LOG_FILE_PATH)

2.3 zabbix WEB configuration

  • alarm medium

insert image description here

  • User Configurable Alarm Media

insert image description here

  • Can be applied in action

3. Delete Elasticsearch indexes X months ago every week

Example of index format: index-2021.11.21

#!/bin/bash
# Filename   :  delete_es_indices.sh
# Date       :  2021/11/21
# Author     :  xxx 
# Email      :  xxx
# Crontab    :  10 00 * * 6 /bin/bash $BASE_PATH/delete_es_indices.sh >/dev/null 2>&1
# Notes      :  将脚本加入crontab中,每周六0点10分执行
# Description:  每周删除Elasticsearch X个月前的索引,索引格式示例:index-2021.04.11


check_args(){
    if [ "$#" -ne 2 ];then
        echo "Usage: $0 [ES_URL] [DEL DAYS]"
        echo "ES_URL示例:http://1.1.1.1:9200,DEL DAYS:表示要删除多少天前的索引。"
        exit 1
    fi
}


# 删除索引函数
DeleteIndices(){
    indices="$1"
    time="$(echo $indices | awk -F'-' '{print $NF}')"
    delete_time="$(date -d "-${DELETE_DAYS_AGO} day" +'%Y-%m-%d')"

    # 过滤时间是不是 2021.04.11 格式的,如果不是就退出函数
    if ! echo "$time" | egrep -o "[0-9]{4}\.[0-9]{2}\.[0-9]{2}" &>/dev/null;then
        return   
    fi

    # 转换为unix好时间,date识别不了2021.11.21日志,所以转换为2021-11-21
    format_time=$(date -d "$(echo $time | tr '.' '-')" +'%s')
    format_delete_time=$(date -d "$delete_time" +'%s')

    # 如果索引时间小于要删除的时间并且索引名字不是monitor开头的,则删除
    if [[ "$format_time" -lt "$format_delete_time" && ! "$indices" =~ ^monitor.* ]];then
        curl -XDELETE "$ES_IP/$indices"
    fi
}

main(){
    check_args $@
    # ES URL
    ES_IP="$1"
    # 删除多少天前的索引
    DELETE_DAYS_AGO="$2"

    curl $ES_IP/_cat/indices | awk '{print $3}' | while read line
    do
        DeleteIndices $line
    done
}

main $@

That's all for today's sharing.

I hope that everyone can apply what they have learned through these cases and apply them in combination with their own actual scenarios, so as to improve their work efficiency.

If you have more script examples, you are also welcome to leave a message to share or leave a message through this article to talk about your specific script example needs. If there are too many examples, next time Jiege will share with you the whole collection of script article examples.

Guess you like

Origin blog.csdn.net/jake_tian/article/details/121559424