2019年11月24日 星期日

Linux 系統資源監控 top mpstat sar uptime

top 監控整體

top裡按1,看每個cpu使用狀況
top裡按2,看每個node使用狀況


//如果是centos,以下須先安裝 yum -y install sysstat

iostat 監控 cpu 及 block 設備(及分割槽) io

# iostat -xz 1 
Linux 2.6.32-696.el6.x86_64 (DX_JQ_2) 12/07/2018 _x86_64_   (32 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle
           0.05 0.00 0.07 0.01 0.00 99.87

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 3.29 0.02 1.13 1.28 35.30 31.88 0.02 20.90 7.84 21.13 5.19 0.60
dm-0 0.00 0.00 0.02 4.41 1.27 35.29 8.25 0.07 14.68 8.78 14.70 1.34 0.60

avg-cpu: %user %nice %system %iowait %steal %idle
           0.06 0.00 0.06 0.00 0.00 99.87
iostat [參數] [間隔秒][次數] 
iostat -x 1 10 //監控詳細信息,間隔1秒,共10次
iostat -C
iostat -dk

    -C 显示 CPU 使用情况
    -d 显示磁盘使用情况
    -k 以 KB 为单位显示
    -m 以 M 为单位显示
    -N 显示磁盘阵列(LVM) 信息
    -n 显示 NFS 使用情况
    -p[磁盘] 显示磁盘和分区的情况
    -t 显示终端和 CPU 的信息
    -x 显示详细信息
    -V 显示版本信息

監控cpu的屬性
%user  CPU 处在用户模式下的时间百分比
%nice  CPU 处在带 NICE 值的用户模式下的时间百分比
%system  CPU 处在系统模式下的时间百分比
%iowait  CPU 等待输入输出完成时间的百分比
%steal  管理程序维护另一个虚拟处理器时,虚拟 CPU 的无意识等待时间百分比
%idle  CPU 空闲时间百分比

監控disk的屬性
Device  监测设备名称
rrqm/s  每秒需要读取需求的数量
wrqm/s  每秒需要写入需求的数量
r/s  每秒实际读取需求的数量
w/s  每秒实际写入需求的数量
rsec/s  每秒读取区段的数量
wsec/s  每秒写入区段的数量
rkB/s  每秒实际读取的大小,单位为 KB
wkB/s  每秒实际写入的大小,单位为 KB
rMB/s  每秒实际读取的大小,单位为 MB
wMB/s  每秒实际写入的大小,单位为 MB
avgrq-sz  需求的平均大小区段
avgqu-sz  需求的平均队列长度
await  等待 I/O 平均的时间(milliseconds)
svctm  I/O 需求完成的平均时间
%util  被 I/O 需求消耗的 CPU 百分比


tps屬性
tps  该设备每秒的传输次数
kB_read/s  每秒从设备读取的数据量
kB_wrtn/s  每秒向设备写入的数据量
kB_read  读取的总数据量
kB_wrtn  写入的总数据量

mpstat 監控cpu

mpstat -A //監控所有狀態
mpstat -P 0/1/ALL   // 指定cpu編號
mpstat -P ALL 2 3  //每兩秒產生一次所有cpu資料,共3次


範例 mpstat -P All 2

11時38分44秒  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11時38分46秒  all   55.25    0.00    1.00    0.75    0.00    0.25    0.00    0.00    0.00   42.75
11時38分46秒    0   10.10    0.00    1.52    1.52    0.00    0.00    0.00    0.00    0.00   86.87
11時38分46秒    1  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00

%user      在internal时间段里,用户态的CPU时间(%),不包含nice值为负进程  (usr/total)*100
%nice      在internal时间段里,nice值为负进程的CPU时间(%)   (nice/total)*100
%sys       在internal时间段里,内核时间(%)       (system/total)*100
%iowait    在internal时间段里,硬盘IO等待时间(%) (iowait/total)*100
%irq       在internal时间段里,硬中断时间(%)     (irq/total)*100
%soft      在internal时间段里,软中断时间(%)     (softirq/total)*100
%idle      在internal时间段里,CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间(%) (idle/total)*100

sar 監控cpu,mem,i/o,net io

sar -P All 1 2  //監控所有cpu狀態,間隔1秒,共2次
sar -u 2 3      //監控所有cpu狀態,間隔2秒,共3次
sar -r //記憶體使用統計
sar -R //記憶體整體統計
sar -W //swap統計
sar -b //I/O及傳輸率統計
sar -d //block device statistics. 
       //block device is a kind of file,
       //which represents a device of some kind, with data 
       //that can be read or written to it in block.
       //可用block形式讀寫資料的裝置(linux中都當成檔案)


 -b I/O and transfer rate statistics
 -B Paging statistics
 -d Block device statistics
 -F [ MOUNT ]
  Filesystems statistics
 -H Hugepages utilization statistics
 -I {  | SUM | ALL | XALL }
  Interrupts statistics
 -m { [,...] | ALL }
  Power management statistics
  Keywords are:
  CPU CPU instantaneous clock frequency
  FAN Fans speed
  FREQ CPU average clock frequency
  IN Voltage inputs
  TEMP Devices temperature
  USB USB devices plugged into the system
 -n {  [,...] | ALL }
  Network statistics
  Keywords are:
  DEV Network interfaces
  EDEV Network interfaces (errors)
  NFS NFS client
  NFSD NFS server
  SOCK Sockets (v4)
  IP IP traffic (v4)
  EIP IP traffic (v4) (errors)
  ICMP ICMP traffic (v4)
  EICMP ICMP traffic (v4) (errors)
  TCP TCP traffic (v4)
  ETCP TCP traffic (v4) (errors)
  UDP UDP traffic (v4)
  SOCK6 Sockets (v6)
  IP6 IP traffic (v6)
  EIP6 IP traffic (v6) (errors)
  ICMP6 ICMP traffic (v6)
  EICMP6 ICMP traffic (v6) (errors)
  UDP6 UDP traffic (v6)
 -q Queue length and load average statistics
 -r Memory utilization statistics
 -R Memory statistics
 -S Swap space utilization statistics
 -u [ ALL ]
  CPU utilization statistics
 -v Kernel table statistics
 -w Task creation and system switching statistics
 -W Swapping statistics
 -y TTY device statistics

uptime

檢視服務器負載狀況
uptime
 22:37:31 up 65 days, 3:15, 1 user, load average: 40.00, 25.01, 10.05
檢視當前服務器1min, 5min, 15min 平均負載
是cpu負載、IO負載的整體評估
如果1min 的值大於 15min的值,表示負載越來越高。

dmesg

檢視資訊
$ dmesg |tail
[ 2567.534310] IPv6: ADDRCONF(NETDEV_UP): em3: link is not ready
[ 2570.386536] tg3 0000:02:00.0 em3: Link is up at 1000 Mbps, full duplex
[ 2570.386550] tg3 0000:02:00.0 em3: Flow control is off for TX and off for RX
[ 2570.386555] tg3 0000:02:00.0 em3: EEE is disabled
[ 2570.386584] IPv6: ADDRCONF(NETDEV_CHANGE): em3: link becomes ready
[20907.774686] perf: interrupt took too long (2502 > 2500), lowering kernel.perf_event_max_sample_rate to 79000
[50276.167405] perf: interrupt took too long (3173 > 3127), lowering kernel.perf_event_max_sample_rate to 63000
[96662.454537] perf: interrupt took too long (4010 > 3966), lowering kernel.perf_event_max_sample_rate to 49000
[143249.558510] perf: interrupt took too long (5025 > 5012), lowering kernel.perf_event_max_sample_rate to 39000
[204028.503117] perf: interrupt took too long (6286 > 6281), lowering kernel.perf_event_max_sample_rate to 31000

vmstat

檢視虛擬記憶體狀況
記憶體、分頁、IO、cpu
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r b swpd free buff cache si so bi bo in cs us sy id wa st
 0 0 0 3082644 2076 4156120 0 0 1 210 12 9 1 0 99 0 0
 0 0 0 3083180 2076 4156112 0 0 0 473 2231 2420 1 0 99 0 0
 0 0 0 3083212 2076 4156112 0 0 0 12510 3946 3945 1 2 97 0 0
 0 0 0 3083684 2076 4156096 0 0 0 1403 2333 1725 1 0 99 0 0
 1 0 0 3082868 2076 4156388 0 0 0 505 2307 1795 4 1 95 0 0
 0 0 0 3083508 2076 4156040 0 0 0 475 1888 1570 1 1 99 0 0
 0 0 0 3083152 2076 4156040 0 0 0 8090 1694 1910 1 1 99 0 0

procs

r:在cpu上執行和等待執行的程式數量
b:阻塞的程式

memory

swpd:虛擬記憶體使用大小
free:空閒的記憶體大小
buffer:用作 buffer 的記憶體大小
cache:用作 cache 的記憶體大小

swap

si:每秒從交換區寫道記憶體的大小
so:每秒寫入到交換區的大小

IO

bi:block device 每秒接收的 block 數量
bo:block device 每秒傳送的 block 數量

system

in:每秒 cpu 中斷次數
cs:每秒 content 切換次數(越小越好)

CPU

us:使用者的 cpu 時間
sy:系統核心執行 cpu 時間
id:空閒的 cpu 時間
wa:等待 IO 的時間
st:從虛擬機中偷走的時間?

free

檢視系統整體記憶體
$ free -h
    total used free shared buff/cache available
Mem: 7.6G   697M   3.0G   23M   3.9G    6.5G
Swap: 8.0G   0B   8.0G
buffer cache 是block device 的記憶體快取
page cache 是檔案資料的快取

2019年11月13日 星期三

Gitlab and Sourcetree

Generate ssh key

cd ~/.ssh
ssh-keygen -t rsa -C "email@ex.com"  //email 可隨便打
//產生 id_rsa //密鑰
//id_rsa.pub //公鑰
//檔名一定要是id_rsa,因為sourcetree 不能匯入ssh key, 只會找預設的key

Gitlab ssh setting

登入gitlab,
左側欄有SSH Keys,
貼上 id_rsa.pub的內容,
最後的email不用貼

Testing

ssh://git@host/test.git
//user 一定要是git,這是自架的gitlab預設的
//有反應就是設定正確

Sourcetree setting

在sourcetree的設定裡,改成跟ssh 測試一樣。
ssh://git@host/test.git

問題

1 如果domain不行,改用ip
2 記得user 要用git

2019年10月23日 星期三

go modules


用途

可以在$GOPHTH外開專案
怕依賴package不見,可以保留依賴檔案(用vendor功能)
同一依賴package,可已有多版本共存在依賴目錄
可以用Goproxy解決有些地方無法使用o get的問題

說明

環境變數要打開 GO111MODULE=on
專案目錄要設定成使用 module : go mod init
專案的外部 package 還是放在$GOPATH/pkg/mod 下
可以保留目前用的外部 package 到 vendor目錄
可選擇使用 vendor 目錄的 package 或用 go mod 抓

流程

  1. 環境變數設置 GO111MODULE=on(使用 go module 模式)
  2. 專案設定使用 module 。 專案目錄下 go mod init。產生 go.mod 檔案。
  3. 安裝外部 package 方法1:go get -u github.com/xxxx/xxx。會更新 go.mod。
    如果 package 沒有在 go.mod 中指定版本,會抓最新的 tag。沒有 tag 會抓最新 commit。
  4. 安裝外部 package 方法2:go mod downloac。下載全部依賴到 $GOPATH/pkg/mod 。
  5. 然後 go 就會到 $GOPATH/pkg/mod 去找需要的外部 package。

原則

  • go get 如果 package 沒有在 go.mod 中指定版本,會抓最新的 tag。沒有 tag 會抓最新 commit。
  • go get 可抓指定的 tag 或 指定的 commit hash。
  • 要更改專案使用的 package 版本,可以直接改 go.mod,然後重新 go get .....
  • 要使用 vendor 目錄的 package: go build, go install, go test 都加上 -mod=vendor 參數。

go111MODULE 設定

  • 只要有設定GO111MODULE=on,就是使用module。
  • 1.13起,專案在 GOPATH 外,且有 go.mod 文件。無論有無設定都等同于 GO111MODULE=on。
  • 專案在 GOPATH 内,且没有 go.mod 文件時,等同於 GO111MODULE=off

go.mod 設定

  • module 定義模組位置
  • require 指定外部 package 及其版本
  • exclude 排除指定的 package
  • replace 只用不同的 package 取代原本的某個 package
module modtest
go 1.12
require (

 github.com/mattn/go-isatty v0.0.8 // indirect
 github.com/sirupsen/logrus v1.4.2
 github.com/ugorji/go v1.1.7 // indirect
 gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
 gopkg.in/yaml.v2 v2.2.2 // indirect
)
exclude github.com/gin-gonic/gin v1.4.0
replace github.com/tedmax100/modtest => ../gomodtest  //用本地目錄或遠端 repo 取代 modtest

go mod 命令

  • go mod init [project dir / or this dir]
  • go mod download
  • go mod tidy
  • go mod vendor
  • go mod edit
  • go mod why

goland IDE

A module 專案時,IDE的 build/test working dir 要在 module 目錄

B module 專案時,IDE的 build/test output dir 不需要在 module 目錄

C Preference/Go/Go Modules(vgo) => Enable



1 Init project to create go.mod

in goland IDE terminal
>go mod init
will create go.mod

2 Create go.sum

in goland IDE terminal
>go test / go build
will create go.mod

or go mod tidy
will create go.mod and add / remove dependencies

go.mod 裡面會紀錄這個project用到的dependencies及版本。
因為是紀錄版本,所以如果作者commit卻沒有tag 版本,那就會對應錯。

3 Dependency 的版本不一定對,要自己確認。

因為dependency 是從某個公開的repository 抓的(多半是github),
他抓的原則是:
a 如果有指定版本,就抓指定版本。
b 抓有tag release版的最新版。

問題

有可能作者commit很多版,但是都沒更新 release tag,這樣就抓不到你要的版本。

方法:

in project direcroty
go get -u github.com/satori/go.uuid@b2ce2384e17bbe0c6d34077efa39dbab3e09123b

這樣會抓回指定commit的版本
go.mod / go.sum裡面,這個 package的版號,會跟著改。

如果是自己寫的package


go.mod檔案的 module 設定要正確,go get 是用這個判斷 module path,
因為 go mod 要從 git server repo 抓 package,
他不會從 local 電腦 的 $GOPATH 抓 package,
所以要把自己寫的 package 也加入 go mod,
要把這個 package 放到 github。
或放到某個自設的 git server 的 repo。

否則使用go mod 會出現  cannot find module providing package  ...... error

go get 抓 github private repository

get get 只使用https
可能需要社 GOPRIVATE 變數
go env -w GOPRIVATE=github.com/abcd

//設定 global config
//好像一定要設成 global, 設local沒用
git config --global url."https://$USERNAME:$ACCESS_TOKEN@github.com".insteadOf "https://github.com"
$USERNAME 就是github username
$ACCESS_TOKEN 就是 user access token, 在 setting => Developer settings => Personal access tokens


2019年8月23日 星期五

Git

Git global setup

git config --global user.name "cruise"
git config --global user.email "cruisecg.cg@gmail.com"

Create a new repository

git clone https://gitlab.3ag.xyz/backend/service.git
cd service
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Push an existing folder

cd existing_folder
git init
git remote add origin https://gitlab.3ag.xyz/backend/service.git
git add .
git commit -m "Initial commit"
git push -u origin master

Push an existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.3ag.xyz/backend/service.git
git push -u origin --all
git push -u origin --tags

用ssh 取代 https

教學
//可以每次手打
git remote set-url origin git@gitlab.com/...

//globally config (~/.gitconfig) or per-repo (.git/config)
git config url.ssh://git@github.com/.insteadOf https://github.com/
git config url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/

or
git config --global url.ssh://git@github.com/.insteadOf https://github.com/
git config --global url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/

//這幾種格式都試一下
git config url.git@github.com/.insteadOf https://github.com/
git config url.git@github.com:.insteadOf https://github.com/
git config url.ssh://git@github.com:.insteadOf https://github.com/

檔案

檔案狀態
1 untracked  commit過後加入的檔案,預設沒有 track
2 tracked    有 track 的檔案,有任何變更,會追蹤
3 unstaged   有追蹤,但是沒有加入預備區(預備提交)
4 staged     有加入預備區
5 uncommitted  在預備區,尚未提交
6 committed    已經提交(push 就會上到 remote repos)

變更檔案狀態
改變檔案狀態,都是使用 add 命令。無論是要 track , stage 檔案,都是 add 。
add 之後,檔案會變成 uncommitted 狀態。

2019年5月29日 星期三

Zabbix



安裝流程

1 安裝server
2 安裝 mysql
3 建立 db
4 改設定

參考官方安裝文件
https://www.zabbix.com/download?zabbix=4.0&os_distribution=centos&os_version=7&db=mysql

https://www.zabbix.com/documentation/4.0/manual/installation/install_from_packages/rhel_centos

設定 zabbix mysql password

grant all privileges on zabbix.* to zabbix@localhost identified by 'password';
flush privileges;

設定 php time zone

vi /etc/httpd/conf.d/zabbix.conf,
php_value date.timezone Asia/Shanghai

2019年5月16日 星期四

MySQL InnoDB Lock

詳細說明
詳細說明

InnoDB在transaction中,會視語法不同,自動加鎖。
Transaction中的鎖,都是以Row為標的。

非transaction中,因為會自動COMMIT,所以使用加鎖的語法也沒用。

InnoDB三種加鎖類型

  • Record lock: This is a lock on an index record.
  • Gap lock: This is a lock on a gap between index records, or a lock on the gap before the first or after the last index record.
  • Next-key lock: This is a combination of a record lock on the index record and a gap lock on the gap before the index record.
  • 官網說明:http://dev.mysql.com/doc/refman/5.7/en/innodb-record-level-locks.html
  • 重點1:Next-key locking combines index-row locking with gap locking.
  • InnoDB performs row-level locking in such a way that when it searches or scans a table index, it sets shared or exclusive locks on the index records it encounters (select 過程搜尋遇到的資料列都會被加鎖)

InnoDB鎖定模式

  • 共享鎖定 (shared lock) (S lock):permits the transaction that holds the lock to read a row.
  • 排他鎖定 (exclusive lock) (X lock):permits the transaction that holds the lock to update or delete a row.

不同語句的加鎖效果

  • SELECT ... FROM 基本不加鎖
    但在 SERIALIZABLE 隔離層級時,會自動在遇到的資料加鎖
  • SELECT ... FROM ... LOCK IN SHARE MODE (加共享鎖)
  • SELECT ... FROM ... FOR UPDATE (加排它鎖)
  • UPDATE ... WHERE ... (加排它鎖)
  • DELETE FROM ... WHERE ... (加排它鎖)
  • INSERT (加排它鎖)

MySQL AUTOCOMMIT命令

Autocommit 是設定MySQL是否自動提交變更

AUTOCOMMIT 的設定值,預設一般都是 1

查詢目前 AUTOCOMMIT 的設定值:SELECT @@AUTOCOMMIT

將 AUTOCOMMIT 改為 0 時 ( SET AUTOCOMMIT=0 ),就算沒使用transaction( START TRANSACTION 或 BEGIN ),整個連線執行的 SQL 指令,都會等到下達 COMMIT 提交後,才會真正儲存變更。

也就是當 AUTOCOMMIT=0 時,跟在交易模式下相同。

更改COMMIT狀態指令:
SET AUTOCOMMIT=0; // AUTOCOMMIT=1;

提交指令:
COMMIT;