hugo daily push 2019-04-23 22:00:01

This commit is contained in:
2019-04-23 22:00:02 +08:00
parent c3d8c76928
commit 75cb692ceb
88 changed files with 6070 additions and 1701 deletions

View File

@@ -0,0 +1,143 @@
---
title: "[筆記] inxi 蒐集詳盡的硬體資訊 / inxi Collect Detail Hardware Info"
date: 2019-04-23T15:28:56+08:00
draft: false
noSummary: false
featuredImage: "https://h.cowbay.org/images/post-default-10.jpg"
categories: ['筆記']
tags: ['linux','bsd','inventory']
author: "Eric Chang"
---
最近因為一直碰到硬碟故障的問題算起來那一批同時購買的5X顆 seagate 2T硬碟已經有一半以上故障返修了....
然後又因為一直沒有添購新的硬碟,只能用這些快過保/已過保的撐著
所以最近不斷的在更換機器內的硬碟,而且還沒有熱插拔!
也導致原本負責處理盤點資產的同事困擾,因為跟手邊的紀錄已經對不起來了
然後就變成要對資產的時候,需要一台一台登入,然後去下不同的指令,取得想要的硬體資訊,超級麻煩的!
<!--more-->
幾次之後終於決定透過ansible來做這件事
一開始的想法很簡單,就用 lshw/dmidecode這些指令去做
可是因為手邊的機器有ubuntu 18.04/16.04/14.04 , Debian 9 , Proxmox (based on debian ) , CentOS , FreeNAS
而有些系統預設沒有 lshw / dmidecode (對FreeNAS 就是說你)
所以變成要依照系統不同去下不同的指令雖然都是ansible在跑但是看到playbook的內容就很煩啊
然後就不小心讓我翻到了 inxi 這個指令,根本就是救星啊!
直接來看輸出的範例
![sample of inxi output](http://i.imgur.com/OSx9cnz.png)
有沒有,是不是很優!
而且簡單易懂,還能抓到同事想看的資料,像是廠牌、型號、序號、記憶體類型(DDR2/3/4)
所以馬上捨棄 lshw/dmidecode ,改用 inxi 來跑
ansible role 的內容也很簡單
就偵測完之後,把結果送出給設定好的收件人
只是因為系統不同,大致上要分成 ubuntu/debian/centos 以及 freebsd 兩種
所以同樣的task 要跑兩次一個要帶sudo 一個不用帶
然後BSD系列的機器在inventory 裡面要帶入 ansible_ssh_user
就這樣,沒有什麼太困難的
```YAML
######### use inxi instead ##################
- name: copy inxi binary to remote Ubnutu/Debian
become: yes
become_method: sudo
copy:
src: inxi
dest: /usr/local/bin/inxi
mode: a+rx,u+rwx
when: ansible_distribution == "Ubuntu" or ansible_distribution == "Debian" or ansible_distribution == "CentOS"
- name: copy inxi binary to remote FreeBSD
copy:
src: inxi
dest: /usr/local/bin/inxi
mode: a+rx,u+rwx
when: ansible_distribution == "FreeBSD"
- name: run inxi to collect Ubuntu/Debian hardware info
become: yes
become_method: sudo
shell: "/usr/local/bin/inxi -c -Dxx -C -m -Z"
register: du_hw_info
when: ansible_distribution == "Ubuntu" or ansible_distribution == "Debian" or ansible_distribution == "CentOS"
- name: run inxi to collect FreeBSD hardware info
shell: "/usr/local/bin/inxi -c -Dxx -C -m -Z"
register: bsd_hw_info
when: ansible_distribution == "FreeBSD"
- name: set Ubuntu/Debian inventory file
template:
src: etc/inventory.txt.j2
dest: "/tmp/{{ ansible_hostname }}_inventory.txt"
mode: a+r,u+rw
when: ansible_distribution == "Ubuntu" or ansible_distribution == "Debian" or ansible_distribution == "CentOS"
- name: set FreeBSD inventory file
template:
src: etc/freenas_inventory.txt.j2
dest: "/tmp/{{ ansible_hostname }}_inventory.txt"
mode: a+r,u+rw
when: ansible_distribution == "FreeBSD"
- name: send inventory file via mail
tags: mail
mail:
host: 192.168.11.173
port: 25
secure: starttls
subject: "{{ ansible_hostname }} inventory file"
from: ansible
to: "{{ recipient }}"
#body: "{{ mail_body.stdout_lines }}"
attach: "/tmp/{{ ansible_hostname }}_inventory.txt"
```
inventory 內容
```
hqs01.abc.com ansible_ssh_host=192.168.11.1
hqs210.abc.com
hqs230.abc.com
hqs231.abc.com
hqs234.abc.com
hqs03.abc.com
hqs020.abc.com
hqs019.abc.com
hqs010.abc.com
hqs05.abc.com
hqs173.abc.com
###BSD Hosts ###
hqs099.abc.com ansible_ssh_host=192.168.11.99 ansible_ssh_port=22 ansible_ssh_user=root
hqs202.abc.com ansible_ssh_host=192.168.11.202 ansible_ssh_port=22 ansible_ssh_user=root
bbs089.abc.com ansible_ssh_host=192.168.0.89 ansible_ssh_user=root
```
ansible 又發揮了一次,另外,感覺這個指令可以用來寫資產管理系統耶...威力強大
而且又不用管作業系統是什麼,反正有執行檔,直接派過去 remote 端就好了!
真是讓我相見恨晚啊!

View File

@@ -0,0 +1,76 @@
---
title: "[筆記] 紀錄所有下過的指令、時間 / Log All commands with timestamp"
date: 2019-04-23T15:08:36+08:00
draft: false
noSummary: false
featuredImage: "https://h.cowbay.org/images/post-default-11.jpg"
categories: ['筆記']
tags: ['log']
author: "Eric Chang"
---
今天發生一件有點詭異的事情,本來應該要經過某個指令才會產生的檔案
居然不知為何自己產生了,在我記憶中沒有去執行過那個指令
翻了一下 bash_history ,裡面也只有下過哪些指令,沒有紀錄時間,完全沒有參考價值(攤手)
所以翻了一下網路至少把這兩台主要跑ansible的機器的log功能補上紀錄所有指令以及時間的部份
<!--more-->
參考這個網頁
![https://askubuntu.com/questions/93566/how-to-log-all-bash-commands-by-all-users-on-a-server](https://askubuntu.com/questions/93566/how-to-log-all-bash-commands-by-all-users-on-a-server)
我沒有打算要紀錄「所有」使用者的指令,只要看有權力執行重要指令的帳號就好
所以先用minion(管理用的帳戶)登入後
先編輯 ~/.bashrc
加入
```
export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$(whoami) [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"'
```
因為這邊用到syslog 的 local6所以要跟著修改 syslog的設定
```
sudo vim /etc/rsyslog.d/bash.conf
加入這行
local6.* /var/log/commands.log
接著設定讓/var/log/commands.log 也能夠自動輪替
sudo vim /etc/logrotate.d/rsyslog
在適當的位置 加入 /var/log/commands.log
然後重起 rsyslog
sudo service rsyslog restart
```
用 minion 登出登入後,就可以看到所有指令都被完整的紀錄下來了
```
sudo cat /var/log/commands.log
2019-04-23 15:18:48 [minion@hqs010 ~]$ sudo cat /var/log/commands.log
Apr 23 15:06:51 hqs010 minion: minion [30832]: [0]
Apr 23 15:06:53 hqs010 minion: minion [30832]: ls -lart [0]
Apr 23 15:06:55 hqs010 minion: minion [30832]: ls -alrt /tmp/ [0]
Apr 23 15:06:58 hqs010 minion: minion [30832]: ls -lart /var/log/ [0]
Apr 23 15:07:07 hqs010 minion: minion [30832]: sudo cat /var/log/commands.log [0]
Apr 23 15:07:13 hqs010 minion: minion [30832]: ls -lart /tmp/ [0]
Apr 23 15:07:18 hqs010 minion: minion [30832]: cat /tmp/hqs010_inventory.txt [0]
Apr 23 15:07:22 hqs010 minion: minion [30832]: cd [0]
Apr 23 15:07:22 hqs010 minion: minion [30832]: ls [0]
Apr 23 15:07:24 hqs010 minion: minion [30832]: ls -lart [0]
Apr 23 15:07:28 hqs010 minion: minion [30832]: ls .inxi/ [0]
Apr 23 15:07:35 hqs010 minion: minion [30832]: clear [0]
Apr 23 15:18:48 hqs010 minion: minion [30832]: ip addr [0]
2019-04-23 15:18:55 [minion@hqs010 ~]$
```
裡面應該會看到滿滿的 cd / ls / cat 吧 XD