Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use agent based on go language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to use agent based on go language. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

An introduction

When building an automated database operation and maintenance system, there must be an agent on the database server to execute the commands initiated by the web server. We have studied several technologies such as Celery,Redis Queue or implementation based on socket, and of course, write it ourselves, because some colleagues have completed an agent---servant before. After communicating with colleagues, we decided to reuse servant so that we did not have to repeat the wheel. Servant is an agent based on go language, which is called by http protocol, provides authority authentication and remote call, and supports asynchronous command execution. It meets our current task of database backup, collecting database metadata information regularly, and verifying the validity of backup regularly. This article is a how to document, which describes how to install and use servant in relative detail. I hope it will be helpful to readers.

Second installation

2.1 Software preparation

Because the agent is written in the go language, install the go language pack

Yum install-y go

Cd / opt/

Git clone https://github.com/xiezhenye/servant.git

Cd / opt/servant

Mode one: make rpm

Mode 2 make

2.2 Directory structure

View the main directory structure after compilation

Binary files compiled by bin #

Conf # profile directory

Example #

README.md

Scripts # servantctl execution file is used to start, stop, check status, etc.

Src # source code file

Operation commands for maintaining servant

/ opt/servant/scripts/servantctl (start | stop | restart | status | help)

When you encounter an error during startup, please go to / data/logs/servant/servant.log to check the information of log.

2.3 detailed description of configuration files

There is a configuration file servant.xml in / opt/servant/conf by default.

: 2465 # listening port

# whether permission verification is enabled when calling. It is recommended to enable it in production environment.

30 # timeout when starting permission verification. If the time exceeds 30s, the call is considered invalid.

/ data/logs/servant/servant.log # log directory log, which is a log directory with a like standard. Other friends need to adjust it appropriately in their own environment.

Date > > / tmp/timer.log

]] >

# File manipulation class, similar to commands, can configure multiple commands to manipulate files, mainly including getting file contents, creating files, deleting files, and reading specified byte ranges

Root indicates that you have permission to access the specified directory, in this case, to access the files in the / tmp/ directory.

/ tmp/

Get

Head

Post

Delete

Put

Log-bin\.\ d + # regular expression

# this is less used to access the database

Select 1

#

Bar

${world}

# when used with auth=true, you must use the user specified in the configuration file when accessing, otherwise you cannot call servant

SomeKey

192.168.1.0 ip 24 # specifies the ip source address that allows access to the ip. It is generally recommended to use local calls for greater security.

The above explains the commonly used configurations. For more detailed explanation, please refer to servant's readme.md.

2.4 for the convenience of testing, remove the authority authentication first.

Comand supports both get and post calls.

[root@rac4 22:38:05 / opt/servant/conf/extra]

# curl http://127.0.0.1:2465/commands/db1/foo

Hello world mysql

[root@rac4 22:40:07 / opt/servant/conf/extra]

# echo "hello world" | curl-XPOST http://127.0.0.1:2465/commands/db1/grep-d @-

Hello world

[root@rac4 22:40:08 / opt/servant/conf/extra]

# echo "hxxello world" | curl-XPOST http://127.0.0.1:2465/commands/db1/grep-d @-

File type operation

Get the contents of the file

[root@rac4 22:38:00 / opt/servant/conf/extra]

# curl http://127.0.0.1:2465/files/db1/test/yz.log

Youzan, nihao, yangyi dba

Create a file

[root@rac4 22:41:56 / opt/servant/conf/extra]

# curl-XPOST http://127.0.0.1:2465/files/db1/test/54.txt-d "hello world"

Verify the writes above

[root@rac4 22:42:03 / opt/servant/conf/extra]

# curl http://127.0.0.1:2465/files/db1/test/54.txt

Hello world

Update file contents

[root@rac4 22:45:13 / opt/servant/conf/extra]

# curl-XPUT http://127.0.0.1:2465/files/db1/test/54.txt-d "yangyi dba"

[root@rac4 22:45:26 / opt/servant/conf/extra]

# curl http://127.0.0.1:2465/files/db1/test/54.txt

Yangyi dba

Enable permission verification. It is recommended to enable permission verification from a security point of view in production environment.

Modify the configuration file to enable auth for true and set user configuration

[root@rac4 22:16:50 / opt/servant/conf]

# uri='/commands/db1/foo'

# ts=$ (date +% s)

# key=someKey

# curl-H "Authorization: ${user} ${ts} $(echo-n" ${user} ${key} ${ts} GET$ {uri} "| sha1sum | cut-F1-d'')"http://127.0.0.1:2465${uri}"

[root@rac4 22:30:30 / opt/servant/conf]

Log error execution failed because the actual time of ts is 22:16:50, the actual time of execution is 22:30:30 and the timeout is 30s, so the call failed

22:30:29 on 2017-05-05 INFO (6) [commands] + 127.0.0.1 GET / commands/db1/foo 42798

22:30:30 on 2017-05-05 WARN (6) [commands]-auth failed: timestamp delta too large

Reset time ts executed successfully again.

[root@rac4 22:30:58 / opt/servant/conf]

# ts=$ (date +% s)

[root@rac4 22:31:02 / opt/servant/conf]

# curl-H "Authorization: ${user} ${ts} $(echo-n" ${user} ${key} ${ts} GET$ {uri} "| sha1sum | cut-F1-d'')"http://127.0.0.1:2465${uri}"

Hello world mysql

Log output

22:31:05 on 2017-05-05 INFO (7) [commands] + 127.0.0.1 GET / commands/db1/foo 42808

22:31:05 on 2017-05-05 INFO (7) [commands] command: [bash-c echo "hello world $(whoami)"]

22:31:05 on 2017-05-05 INFO (7) [commands] process started. Pid: 27706

22:31:05 on 2017-05-05 INFO (7) [commands]-execution done

2.5 problems encountered during installation

1 need to create when installing

Mkdir-p / opt/servant/conf/extra

2 the problem of authentication authority

Because the default / opt/servant/conf/servant.xml auth = true, it needs to be changed to false.

Otherwise use curl to execute the command

Curl http://127.0.0.1:2465/commands/db1/foo

An error was reported in the log.

21:52:30 on 2017-05-05 INFO (3) [commands] + 127.0.0.1 INFO 41988 GET / commands/db1/foo

21:52:31 on 2017-05-05 WARN (3) [commands]-auth failed: bad auth header

On how to use agent based on go language to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report