上文回顾
书接上文, 看完上篇文章的同学相信大家对这个”oxidized” 有了初步的了解,
有同学对config 配置有些疑惑 我这里简单介绍一下。
---
username: username : 用户名 这个参数不用改,会从router.db读取.
password: password : 密码 这个参数也不用改,会从router.db读取.
model: junos : 模型 这个参数也不用改,会从router.db读取.
interval: 3600 : 备份周期 单位是秒,也就是默认一个小时备份一次
log: /home/oxidized/.config/oxidized/logs/oxidized.log:日志
debug: false :debug模式
threads: 30 :线程
timeout: 20 : 超时
retries: 3 : 重试次数
prompt: !ruby/regexp /^([\w.@-]+[#>]\s?)$/
rest: 127.0.0.1:8888 :web 用到
next_adds_job: false
vars: {}
groups: {}
models: {}
pid: "/home/oxidized/.config/oxidized/pid"
input: 连接设备模式
default: ssh, telnet
debug: false
ssh:
secure: false
output: 导出配置模式
default: file
file:
directory: "/home/oxidized/.config/oxidized/configs"
source:
default: csv
csv:
file: "/home/oxidized/.config/oxidized/router.db"
delimiter: !ruby/regexp /:/
map: 这个地方和router.db里面的参数对应 从0开始
name: 0
model: 1
username: 2
password: 3
gpg: false
model_map:
cisco: ios
juniper: junos
配置对比
原理简介
Oxidized使用了 rugged/libgit2 接口,
是把output 设置成git , 使用git diff 来实现对比。
配置步骤
值需要修改output部分的配置,就是这么的简单
output:
default: git
git:
user: Oxidized
email: o@example.com
repo: " /home/oxidized/.config/oxidized/oxidized.git "
这样就配置好了,是不是简单的不敢相信
验证结果
我们来点击一下 下图红色框里面的图标
是不是发现有变化了。
让我们来修改一下 这台设备配置,删掉一条snmp配置
然后为了尽快看到效果 我们在手动 update一下。
这个时候已经可以看到 有2个版本的配置了。
点击一下 上图红色图标 然后我们就可以清楚看到 配置对比。
Hooks
理论介绍
为什么要在这里介绍这个呢,发送邮件,还有发送配置到git远程仓库 都用到这个配置,
我们先来了解下什么是 hooks ,俗称钩子,对于广大网络玩家 第一次看到这个词的时候 通常是如下表情:
我们来看图了解一下 这个在程序员世界里面常用的技能,
Hooks就像一些外来的钩子,在源代码之间钩取(窃听)一些信息,当它捕捉到自己感兴趣的事发生,就拦截下来,让自己的代码执行一下,处理一下这个信息,然后再放出去继续之前的进程。这样就可以在不用改变源代码的情况下,做一些别的事情,比方说监控、分析和一些恶意的事。
瞬间感觉这个”钩子”好强大,好方便,这样是不是稍微有点理解了呢。
发送邮件
官方实现方式
前期准备
Oxiduzed提供了一个扩展的脚本
oxidized/extra/oxidized-report-git-commits
脚本也给出了配置案例
hooks:
email_output:
type: exec
events: [post_store, node_fail]
cmd: '/home/oxidized/extra/oxidized-report-git-commits | mail -s "Oxidized updates for ${OX_NODE_NAME}" update-recipient@example.com'
async: true
timeout: 120
配置是比较简单 稍微简单介绍一下这个 email_output hooks
Type:exec :就是触发这个hooks时候 可以执行shell命令 或者 其他二进制文件
Events:一共有4个参数
node_success:节点备份成功
node_fail:节点备份失败
post_store:节点配置修改
nodes_done:所有节点备份完成
可以看出案例中
当备份失败或者有配置修改的时候就会触发这个钩子
然后就会发送邮件。
系统配置
ubuntu 一般会默认安装一个叫做postfix 的邮件服务。没有就安装一下。
apt install postfix
systemctl restart postfix.service
apt install mailutils 这个软件就是提供脚本中用到mail命令的
配置步骤
先把脚本复制到指定目录
cp –r /var/lib/gems/2.3.0/gems/oxidized-0.21.0/extra/
/home/oxidized/.config/oxidized/
然后修改官方给的配置案例, 其实也就是 指定一个邮箱地址。
hooks:
email_output:
type: exec
events: [post_store, node_fail]
cmd: ' /home/oxidized/.config/oxidized/extra/oxidized-report-git-commits | mail -s "Oxidized updates for ${OX_NODE_NAME}" 191801737@qq.com'
async: true
timeout: 120
验证结果。
然后我们修改一下 设备的配置:
然后登陆我的QQ邮箱, 会默默的发现一个 配置变更邮件。
第三方实现方式
第三方实现方式 说白了就是用像公司邮箱或者新浪邮箱来发送配置变更邮件。
系统配置
先停掉postfix
systemctl stop postfix.service
我们来使用一个强大的软件 heirloom-mailx
apt–get install heirloom–mailx
配置heirloom-mailx 在s-nail.rc 文件里面添加。
vim /etc/s-nail.rc
set from=USER@sina.com
set smtp=smtp.sina.com
set smtp-auth-user=USER
set smtp-auth-password=PASSWORD
set smtp-auth=login
先用命令调试一下:
echo “邮件内容” | heirloom-mailx -vs “邮件标题” 123@qq.com
配置步骤
hooks:
email_output:
type: exec
events: [post_store, node_fail]
cmd: '/home/oxidized/.config/oxidized/extra/oxidized-report-git-commits | heirloom-mailx -s "Oxidized updates for ${OX_NODE_NAME}" 123@qq.com'
async: true
timeout: 120
验证结果
我是发到公司的邮箱,只要把收件人换成你们小组的邮箱 让整个组的人都收到配置变更邮件。是不是感觉棒棒哒。
Git 远程仓库
有的小伙伴可能会有疑问 ,不是已经把备份文件output 到git了么 ,那么这个远程git又是什么鬼?
举个栗子:这个就像 电脑的本地磁盘和百度云盘 一样 一样的。其实妙处用过github的小伙伴 心中了然。
前期准备
创建SSH Key
ssh–keygen –t rsa –C “GitLab”
–b 4096
在用户主目录下 会有 .ssh/id_rsa和.ssh/id_rsa.pub这两个文件
我用的公司的gitlab
打开”Account settings”,”SSH Keys”页面:
然后,点”Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容
配置步骤
我是把备份成功和有备份差异的 文件都会发送到公司的gitlab.
push_to_remote:
type: githubrepo
events: [node_success, post_store]
single_repo: true
remote_repo: git@git.xxxxxx.com:it-ops/oxidized.git
publickey: /home/oxidized/.config/.ssh/id_rsa.pub
privatekey: /home/oxidized/.config/.ssh/id_rsa
验证结果
我们在去gitlab上面看看 。
Web登陆验证
安全是永恒的话题
前期准备
安装软件
apt install apache2–utils
新建一个用户
htpasswd –c /etc/apache2/.htpasswd user1
然后根据提示输入2次密码
如果你需要新建第二个用户 用下面的命令
htpasswd /etc/apache2/.htpasswd user2
我们来确认一下刚才创建了2个用户信息
$ cat /etc/apache2/.htpasswd
user1:$apr1$/woC1jnP$KAh0SsVn5qeSMjTtn0E9Q0
user2:$apr1$QdR8fNLT$vbCEEzDj7LyqCMyNpSoBh/
配置步骤
配置Ngixn文件
root@node-2:~# vim /etc/nginx/conf.d/oxidized.conf
server {
listen 80;
listen [::]:80;
server_name 10.100.18.141;
auth_basic "oxidized web UI login";
auth_basic_user_file /etc/apache2/.htpasswd;
location / {
proxy_pass http://127.0.0.1:8888/;
}
access_log /var/log/nginx/access_oxidized.log;
error_log /var/log/nginx/error_oxidized.log;
}
验证结果
输入我们刚才创建的用户名和密码 进入系统
To be continued
这篇看下来 是不是觉得对oxidized印象又丰富了不少。
Advanced Group Configuration
细心的同学 可能会发现 配置文件config中有很多参数我们都没有用到,比如说groups,models。那么这些 被官方称作为 Advanced Configuration 的内容 有什么优势,使用上面又有什么限制呢?会放在下篇文章聊聊。
Source: Mysql
需要备份的设备达到一定数量级(数百台+)的时候,就不在适合用router.db 作为source了,这个时候数据库 闪亮登场。
下篇文章会详解 怎么通过Python脚本 调用公司的CMDB接口。实现设备周期的自动化备份操作。感觉逼格又高了一点呢。
Removing secrets
对于那些想用 git远程仓库,但是公司又没有git服务器的同学,
我们把数据脱敏后 放到github上面 也不是不可以。