【自动化】第二篇-shell流程自动化

图片

图片来自网络

摄影|Bestviewstock

----------------------------------------------------------◇ 背景 ◇---------------------------------------------------------

最近,运维的同学经常写一些东西到linux服务器上,我需要定期的将这些东西(日志)获取到,然后处理发送到微信群里。(【打卡】友好机器人🤖给你提醒,企微用起来

第一步想到的就是:如果文件下产生了日志,运维的同学就通知我,我立马一顿操作猛如虎,然后运行几个脚本,带几个参数,搞定。但是有更加自动化的方法吗?

有!

#1

开动脑筋

这种场景我似乎遇到过很多次了。核心涉及用一个文件标记📌上一状态T0,然后不断检测新的日志文件,只要当前检查状态T1和上一状态T0不一致,则产生新的动作,更新标记。否则,就不执行操作。

图片

最好用来比较同时又能够区别不同的文件的就是时间戳了。${new_version}记录了当前最新的文件,${last_version}标记了上一最新版本文件。

last_version=$(cat last_version)if ((${new_version} > ${last_version})); then    echo "写入成功"    $python wechat_robot.py    echo ${new_version} >"last_version"else    echo "fi"fi

#2

再进一步

但是如何拿到最新的文件呢?

这个在python里面好办,直接获取文件列表,正序排序取最后一个就是了。那么shell中类似的是怎么做的呢?

很简单:

file_names=$(ls)file_arr=($file_names)
new_file=${file_arr[-1]}new_version=${new_file:0:10}

$(ls)本身就是排序好了的,转成file_arr后,直接取最后一个${file_arr[-1]}就得到了最新的文件。文件可能有各种各样的后缀(形如1633701622.csv,1633701282.tar.gz等)。那就直接截取前10个得到版本号,就可以直接比较了。

#3

有效封装

wechat_robot.py模块可以进行有效封装,适用大多数场景

def send_msg(msg, send_to, api=webhook):
    post_msg_context = {
        "msgtype": "text",
        "text": {
            "content": msg,
            "mentioned_mobile_list": send_to
        },
    }
    headers = {"Content-Type": "application/json"}
    res = requests.post(api,
                        headers=headers,
                        data=json.dumps(post_msg_context))
  
  def send_img(img, send_to, api=webhook):
    with open(img, "br") as f:
        fcont = f.read()
        # 转化图片的base64
        ls_f = base64.b64encode(fcont)
        # 计算图片的md5
        fmd5 = hashlib.md5(fcont)
    post_img_context = {
        "msgtype": "image",
        "image": {
            "base64": ls_f.decode('utf8'),
            "md5": fmd5.hexdigest(),
            "mentioned_mobile_list": send_to
        },
    }
    headers = {"Content-Type": "application/json"}
    res = requests.post(api,
                        headers=headers,
                        data=json.dumps(post_img_context))

def send_file(filename, send_to, api=webhook):
    id_url = api.replace("send", "upload_media") + '&type=file'  # 上传文件接口地址
    data = {'file': open(filename, 'rb')}  # post jason
    response = requests.post(url=id_url, files=data)  # post 请求上传文件
    json_res = response.json()  # 返回转为json
    media_id = json_res['media_id']  # 提取返回ID

    post_file_context = {
        "msgtype": "file",
        "file": {
            "media_id": media_id,
            "mentioned_mobile_list": send_to
        },
    }
    headers = {"Content-Type": "application/json"}
    res = requests.post(api,
                        headers=headers,
                        data=json.dumps(post_file_context))

 

#4

消息转图片

但是有时候,消息还是图片好看。那就需要常见的两个python库了。prettytable和ImageDraw。

图片

这个库可以有效的把string转成图片。

 

 

感兴趣,可以关注公众号elegantcoin,接受更多消息

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值