Jenkins and Nail-nailing robot realizes the current service log for mobile

Source: Internet
Author: User

Immediately to the new year, you are not only because of the train ticket to buy home and Howl Cry also began to expand the server and adjust the monitoring value in advance, the purpose is to have a quiet spring festival. But this is 10 days or so not in the company, if the module really out of what accident must not be the first to find the log analysis problem, after all, these days in the visit and live in a happy life, go to the back of a notebook is too inconvenient.

So this time, I would like to use the mobile phone to start the server script, so that the script can get the current log, and then return the results to the mobile side. So you don't have to go anywhere with the company laptop that looks like a wet blanket.

Use the phone to start the server script? I'm not going to develop Android and iOS, so I'm definitely going to use a third-party tool, and I'm conditioned to think of Jenkins, because Jenkins uses a mobile phone to log in, so what's the result on the phone? In the public number and nail-nailing robot, I chose the nail-nailing robot.

Creating nail-nailing robots

My nail version is 4.2.6.37, first in the top left corner of the head of the Triangle menu there is a 机器人管理 ,

Then select the custom robot, give it a name and change the icon, add to a group chat,

Add, this robot will generate a webhook , its structure should be: https://oapi.dingtalk.com/robot/send?access_token=XXX , the following xxx is an identifier, different identifiers represent different robots, if lost, you can click on the robot avatar and then choose to 机器人设置 see again.

Writing robot Scripts

The official description of the robot site is that https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.zZIvnt&treeId=257&articleId=105735&docType=1 it has been written in a way that is clear enough. My Python script here is in JSON format, as follows:

    #!/bin/python    #coding: utf-8    import json,urllib2    #这里是机器人对应的Webhook地址    url = "https://oapi.dingtalk.com/robot/send?access_token=这里输入你机器人的标识符    #这里是头,原样复制就好    header = {        "Content-Type": "application/json",        "charset": "utf-8"        }    #这里是传送的消息    data = {         "msgtype": "text",            "text": {                "content": "这里是消息正文!"            },         "at": {             "atMobiles": [            "A的手机号",            "B的手机号"            ],              "isAtAll":False   #这里True代表要发给所有人,False的话,要代表消息只发给A和B这两个人          }     }    sendData = json.dumps(data)    request = urllib2.Request(url,data = sendData,headers = header)    urlopen = urllib2.urlopen(request)    print urlopen.read()

Execute this script directly and you'll see my newly-built nail robot talk in group chat.

The robot is paired with Nginx

The above script has been able to achieve our goal initially, but there is a drawback, is that the text content can not be too long. But I want to print a little more log, at least 50 lines, what to do? I think, you can put the log into a Web page nginx, and then use the nail robot feedback this page address Ah, so that content to write how much can be written.

Suppose I now get to the log file written into a call chairmanmao.html that opens in the browser to look like this:

Then the python script for the robot above will be changed to this:

    #!/bin/python #coding: Utf-8 import json,urllib2,commands commands.getstatusoutput (' echo-e ' This is TEST MES sage! \ n ">/path/chairmantail.html ') #这里可以给网页加一个标题 commands.getstatusoutput (' CAT/path/chairmanmao.txt >>/path/chairma Nmao.html ') #这里就是把诗词写进html文件里 #这里是机器人的webhook地址 url = "Https://oapi.dingtalk.com/robot/send?access_token= enter here         The identifier of your robot "header = {" Content-type ":" Application/json "," CharSet ":" Utf-8 "} data = { "Msgtype": "link", "link": {"text": "Click the URL to get to the results of this log query", "title": "Log query The results have been generated! "," Picurl ":" Http://p1x3hd2at.bkt.clouddn.com/nanshen.jpg ", #这里可以加一个缩略图片" message         URL ":"/HTTP/server extranet IP address/chairmanmao.html "}," at ": {" Isatall ": True # at is not required} } SendData = json.dumps (data) request = Urllib2. Request (url,data = senddata,headers = header) Urlopen = Urllib2.urloPen (Request) print urlopen.read () 

Execute this script to see that the robot sends the following information:

Then open this URL and see the full page information:

Then the Chairman Mao Poetry to replace the actual log file, do not have to print all the logs out, tail -n 50 日志文件名 50 lines enough to use.

Configuring Jenkins

After the script is finished, the robot is finished, and this time it is necessary to add "start-up". The steps to install Jenkins I will not write here, directly to see https://rorschachchan.github.io/2018/02/05/Jenkins安装与创建简单任务/ . Now go to the Jenkins Web page, to add a new job, such as my job is called "Get module Log",

If you want to read the logs of other servers in Jenkins, you can 构建project select them at the time and 参数化构建过程 then configure the parameter IP to pass the IP to the target script. If you feel like this hold, you can not use Jenkins this function, the IP to write to the script, solve everything:

In 构建 that step, select Execute Shell , and then write the specific shell command, if the parameters are used in the above, then the parameters can be used here, my script is no ip this parameter, in the diagram $ip is to do an example to explain the usage:

In 构建后操作 This step you can choose E-mail Notification to send an email notification if it fails. You don't have to choose anything if you don't need it. Then is to save this project, click on the left menu bar 立即构建 , you will see Build History more than one #1 out, at the same time nail the robot also in the group sent messages, this #1 is the construction of records, this record more, the record will overwrite the old records.

Click this #1 , select 控制台输出 , you can see the specific operation results, with the shell interface to perform the same effect. Visible operation success, the purpose has been achieved!

You'll need to call the script later, log in to Jenkins on your mobile browser, and build this project, and you'll see the feedback from the nail-pinning robot.

Resources

Https://xu3352.github.io/linux/2017/05/01/jenkins-restart-remote-server-tomcat

Jenkins and Nail-nailing robot realizes the current service log for mobile

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.