Skip to main content

SA Tools Core

SA Tools 顾名思义是 System Administrator 的工具集,包含一些实用工具,涉及 DNS 管理、远程批量执行脚本、日志分析查询、通知提醒等。

Installation

pip install sa-tools-core

Development Guide

Currently support python3.6+

Quick start

# first clone this repo
cd sa-tools-core

make init

See Makefile for more details.

Re-install after modify codes

make install

Configuration

cp local_config.py.example local_config.py
# then edit local_config.py
vi local_config.py

You can use a system-wide configuration path as well, by default it is /etc/sa-tools/config.py.

Local configs will override sa_tools_core/consts.py.

For other third party service configs, see examples/config.

You can put third party service configs to /etc/sa-tools/ directory.

Command Line Tools

For all the CLI tools, you can type -h or --help to get help messages and examples.

sa-notify

通知提醒工具,支持 wework(企业微信), email, sms, pushbullet, pushover, telegram 等多种通知类型。

sa-notify --wework user1 --content 'xxx'
echo 'xxx' | sa-notify --wework user1,user2 --email user1@example.com user3@example.com

sa-dns

DNS 管理工具,目前仅支持 DNSPod。

# 切 A 记录
sa-dns ensure main --type A --value 1.1.1.1 --enable
# dry-run
sa-dns ensure main --type A --value 1.1.1.1 --enable --dry-run
# 切 A 记录,独占
sa-dns ensure main --type A --value 1.1.1.1 --enable --excl
# 调整 ttl
sa-dns ensure main --type A --value 1.1.1.1 --ttl 100 --enable
# 批量切 CNAME 记录,常用于 CNAME 到 CDN 等操作
sa-dns ensure main --type CNAME --value {domain}.h1.aqb.so. --enable

# 查找子域记录
sa-dns list -S music
# 查找 aqb 相关记录(只返回符合该关键字的记录)
sa-dns list -s aqb
# 按正则查找子域(查看 aqb 的测试域名)
sa-dns list | grep -E '^.*aqb\s'

# 支持通过 -d,--domain 指定其他域名
sa-dns -d dou.bz list

see more

sa-script

A remote script runner tool based on ansible. To use it, you need to prepare your ansible environment first.

远程命令执行工具,目前基于 ansible,需要事先配置好 ansible 环境(/etc/ansible/hosts)。

一些特点,

  • 兼容 ansible host pattern
  • 脚本可从 stdin 传入或指定文件路径,若都不指定则会调用 editor 进入编辑模式(类似 git commit 时的行为)。
  • 批量执行,有进度条
  • 执行完毕后会进入交互模式,可以对结果进行筛选,支持 shell 管道操作,支持再次发起执行
$ echo 'uptime && echo $HOSTNAME $(whoami)' | sa-script test_zk
Executing...
100%|######################################################|Elapsed Time: 0:00:09

+----------+----+-------------------------------------------------------+--------+
| host     | rc | stdout                                                | stderr |
+----------+----+-------------------------------------------------------+--------+
| test-zk3 | 0  |  11:40:15 up 384 days, 19:00,  1 user,  load average: |        |
|          |    | 0.16, 0.20, 0.26 test-zk3 user1                       |        |
| test-zk2 | 0  |  11:40:15 up 392 days, 20:00,  1 user,  load average: |        |
|          |    | 0.25, 0.22, 0.30 test-zk2 user1                       |        |
| test-zk1 | 0  |  11:40:15 up 392 days, 23:53,  1 user,  load average: |        |
|          |    | 0.30, 0.22, 0.25 test-zk1 user1                       |        |
+----------+----+-------------------------------------------------------+--------+

See sa-script -h for more details.

TODO: add a gif to demonstrate.

sa-access

access log 查询分析工具,支持简单查询,聚合查询,时段对比分析等

sa-access query
sa-access query --term ip 1.1.1.1
sa-access query -t host example.com -x bandwidth --by ip
sa-access query --term appname app1 -x count --by-script "doc['remote_addr'].value + ' ' + doc['normalize_url'].value"
sa-access query --term appname app1 -x count --by-script "def ip=doc['remote_addr'].value; ip.substring(0, ip.lastIndexOf('.')) + ' ' + doc['normalize_url'].value"
sa-access analyze --term host example.com -x sum bytes_sent --by nurl -a '2017-03-28 09:30' -d 15 -b '2017-03-28 10:30'

see more

sa-icinga

一个 Icinga2 的 CLI 工具。

icinga2 doc: http://docs.icinga.org/icinga2/latest/doc/module/icinga2/toc

# try test
sa-icinga notify --wework user1 --email user1@example.com --test

sa-icinga notify --wework user1 --email user1@example.com  # need icinga pass os environment vars

sa-icinga ack --host sa --service check-puppet --comment 'hehe'
sa-icinga ack --host 'sa*' --service 'check-puppet'
sa-icinga ack --host 'sa*' --service 'check-puppet' --remove

sa-icinga show --filter 'host.name == "sa" && service.name == "check-puppet"'
sa-icinga show --type host --filter 'match("sa*", host.name)' | less
sa-icinga show --type service --filter 'regex("check_[a-z]*", service.name)' | less
sa-icinga show --type notification --filter 'notification.host_name == "sa"' | less
sa-icinga show --type user | grep user1
sa-icinga show --filter 'service.name == "check-puppet"' --attrs acknowledgement

see more

sa-disk

磁盘相关工具,利用 ncdu 快速扫盘,并可以保存、分析结果。

sa-disk usage
sa-disk usage -n 5 -d 3
sa-disk usage -r /data1/ncdu-export-%-20160513142844.gz
sa-disk usage -c /tmp
sa-disk usage -p /data
sa-disk usage --force-check

sa-disk clean

see more

sa-tc

sa-tc 是对腾讯云 API 的封装,支持黑石、CVM 等产品。

sa-tc bm devices
sa-tc bm devices -f yaml
sa-tc bm devices --alias host

sa-github

sa-github 是对 Github API V3 的封装, 支持 github.com 和 ghe, 目前功能还在完善中, 只支持 collaborator api

sa-github collaborator --org xxx --repo yyy add --username user_1 --permission admin
sa-github collaborator --org xxx --repo yyy remove --username user_1

Client

Tools can be called from client as well.

from sa_tools_core.client import Client

c = Client()
c.notify(wework='user1', content='hehe')
c.uptime()
c.dns.list(S='@')

See sa_tools_core/client.py for more details.

Release files for sa-tools-core 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sa-tools-core 0.6.0
File Size Uploaded
sa-tools-core-0.6.0.tar.gz 53.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sa-tools-core 0.6.0
File Interpreter ABI Platform
sa_tools_core-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 113.9 kB

Release files / sa-tools-core-0.6.0.tar.gz

Download URL sa-tools-core-0.6.0.tar.gz
Size 53.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b17341b6a5c7950557b10463d86c990dee11451844d3b9adcddc492250488810
BLAKE2b-256 checksum
How to use checksums
c19eb62af279043adc7b29169e3e6013b7210da32e3cadc7f2c84788d931a081
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

Release files / sa_tools_core-0.6.0-py3-none-any.whl

Download URL sa_tools_core-0.6.0-py3-none-any.whl
Size 60.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d15953267faf2a2be09128e9885cb94cef4092bf2be45e6e03aa9fe3b5b91f5e
BLAKE2b-256 checksum
How to use checksums
6be9f4ebd74bc7e427b7c6d4dcff4ffa6dd8fdda041f85d94c0877316b24667b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page