Các thông số của CPU trên linux

Các thông số của CPU khi sử dụng lệnh top
Cpu(s): 0.1%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
0.1%us CPU đang sử dụng 0.1% năng lực xử lý cho running user space processes, tức các tiến trình không thuộc về hoạt động của kernel như shell, các trình biên dịch, database, webserver, và các chương trình khác. Nếu CPU không ở trạng thái nghỉ, idle thì thường phần lớn năng lực của nó đang dùng cho các tiến trình user như thế này.

99.7%id Con số này nói cho chúng ta biết, CPU dành 99.7% thời gian của nó để nghỉ ngơi trong lần lấy mẫu gần nhất, không có gì để làm. Tổng của các con số us, id, ni càng gần 100% càng tốt, nếu không, có lẽ có gì đó sai sai đang xảy ra.
0.2%sy Con số này là lượng thời gian mà CPU dành cho xử lý của kernel, tất cả các process và tài nguyên hệ thống được phụ trách bởi linux kernel. Khi các tiến trình của người dùng (user space processes) cần cái gì đó từ hệ thống như xin cấp phát bộ nhớ, đọc ghi, tạo thêm tiến trình con, thì kernel sẽ làm những việc này. Trong thực tế, kernel sẽ tự đặt lịch, quyết định tiến trình nào sẽ chạy trước, cái nào chạy sau. Con số CPU dành cho phần này càng ít càng tốt, nếu nhiều hơn, có lẽ có vấn đề gì đó như I/O đang tăng cao quá mức.
0.0%ni Con số này phần trăm CPU sử dụng để thay đổi độ ưu tiên các process.
0.0%wa Con số thể hiện %CPU đang dành cho việc ngồi đợi I/O
0.0%hi Con số thể hiện %CPU dùng cho việc xử lý phần cứng bị gián đoạn. Phần cứng gián đoạn từ nhiều phạm vi khác nhau như ổ đĩa lởm, đường truyền mạng tậm tịt, etc, gây ra gián đoạn những gì CPU đang xử lý.
0.0%si Con số thể hiện %CPU dùng cho việc xử lý khi có phần mềm bị gián đoạn. Không thường xảy ra ở mức CPU, thường xảy ra từ lớp kernel trở lên.
0.0%st COn số này dành cho các máy chủ ảo, khi linux chạy trong máy ảo, trên lớp hypervisor, số st ( short for stolen) thể hiện bao nhiêu CPU đã sử dụng để đợi thằng hypervisor, khi nó phục vụ tài nguyên cho các con CPU khác. Con số này bắt nguồn từ thực tế rằng, có nhiều processor ảo dùng chung processor vật lý với nhau.

Một số vấn đề với các con số trên: (để nguyên tiếng anh)

High user mode – If a system suddenly jumps from having spare CPU cycles to running flat out, then the first thing to check is the amount of time the CPU spends running user space processes. If this is high then it probably means that a process has gone crazy and is eating up all the CPU time. Using the top command you will be able to see which process is to blame and restart the service or kill the process.

High kernel usage – Sometimes this is acceptable. For example a program that does lots of console I/O can cause the kernel usage to spike. However if it remains higher for long periods of time then it could be an indication that something isn’t right. A possible cause of such spikes could be a problem with a driver/kernel module.

High niced value – If the amount of time the CPU is spending running processes with a niced priority value jumps then it means that someone has started some intensive CPU jobs on the system, but they have niced the task.
If the niceness level is greater than zero then the user has been courteous enough lower to the priority of the process and therefore avoid a CPU overload. There is probably little that needs to be done in this case, other than maybe find out who has started the process and talk about how you can help out!
But if the niceness level is less than 0, then you will need to investigate what is happening and who is responsible, as such a task could easily cripple the responsiveness of the system.

High waiting on I/O – This means that there are some intensive I/O tasks running on the system that don’t use up much CPU time. If this number is high for anything other than short bursts then it means that either the I/O performed by the task is very inefficient, or the data is being transferred to a very slow device, or there is a potential problem with a hard disk that is taking a long time to process reads & writes.

High interrupt processing – This could be an indication of a broken peripheral that is causing lots of hardware interrupts or of a process that is issuing lots of software interrupts.

Large stolen time – Basically this means that the host system running the hypervisor is too busy. If possible, check the other virtual machines running on the hypervisor, and/or migrate to your virtual machine to another host.

Hướng dẫn cài đặt VNC server trên centos


Để sử dụng GUI từ xa của centos, có thể dùng VNC server, sau đây là hướng dẫn:

Lệnh cài đặt:
yum install tigervnc-server -y
chkconfig vncserver on

VNC sử dụng chính user của hệ điều hành, nên cần thêm user thì chỉ cần dùng lệnh useradd như bình thường, tuy nhiên, password thì VNC dùng password riêng. Để đặt password, ví du cho user huybk thì cần:
su - huybk
vncpasswd

Thêm dòng cấu hình vào file: vi /etc/sysconfig/vncservers
VNCSERVERS="1:huybk"
VNCSERVERARGS[1]="-geometry 1280x768"

Khởi động lại service vnc để cấu hình có hiệu lực
service vncserver restart

Phía client: Truy cập http://tigervnc.bphinz.com/nightly/ để download bản vnc client phù hợp
kết nối tới server mới cài ở địa chỉ
IP server:5901

Xử lý lỗi docker – WARNING: IPv4 forwarding is disabled. Networking will not work.


Tạo container docker thì được thông báo:

WARNING: IPv4 forwarding is disabled. Networking will not work.

Nguyên nhân do hệ điều hành host đangtawts IP forwarding, để bật lên, thêm/sửa dòng sau trong /etc/sysctl.conf
net.ipv4.ip_forward=1

Sau đó restart network, hoặc chạy lệnh
sysctl -p

Rsync : Công cụ đồng bộ, copy dữ liệu tuyệt vời cho linux

Rsync (Remote Sync) là một công cụ mạng để sao lưu vào đồng bộ dữ liệu trên linux. Rsync có khả năng đồng bộ file giữa:
– 2 thư mục trong 1 máy chủ local
– 2 thư mục giữa máy chủ local và máy chủ remote
So sánh với scp truyền thống, rsync có những ưu điểm nổi trội:
– Hỗ trợ sao chép link, permission, .v.
– Nhanh hơn, do sử dụng cơ chế nén khi copy
– Có những tính năng như giới hạn băng thông truyền tải, bảo tồn được mốc thời gian, permission nguyên vẹn khi chuyển đi.
Syntax cơ bản:
rsync options source destination
Trong đó:
-v : verbose
-r : sao chép dữ liệu theo cách đệ quy ( không bảo tồn mốc thời gian và permission trong quá trình truyền dữ liệu)
-a :chế độ lưu trữ cho phép sao chép các tệp đệ quy và giữ các liên kết, quyền sở hữu, nhóm và mốc thời gian
-z : nén dữ liệu
-h : định dạng số

Cài đặt:
#Đối với họ redhat
yum install rsync
#Đối với họ debian
apt-get install rsync

Sao lưu, đồng bộ file trên local:
[root@devopsvn.xyz]# rsync -zvh backup.tar /tmp/backups/
created directory /tmp/backups
backup.tar
sent 12.31M bytes received 51 bytes 9.27M bytes/sec
total size is 21.49M speedup is 2.10

Nếu thư mục đích chưa tồn tại, thì rsync sẽ tự tạo folder
Ví dụ về đồng bộ folder 1 và folder 2
[root@devopsvn.xyz]# rsync -avzh /folder1 /folder2/

Đồng bộ dữ liệu giữa local và remote server, hoặc ngược lại:
Ví dụ 1: sao chép folder /tmp/vidu lên thư mục /opt của server 1.2.3.4
[root@devopsvn.xyz]$ rsync -avz /tmp/vidu/ root@1.2.3.4:/opt/
Ví dụ 2: Sao chép /opt/sex/ trên 1.2.3.4 về /opt/sex_backup của local
[root@devopsvn.xyz]# rsync -avzh root@1.2.3.4:/opt/sex/ /opt/sex_backup/

Các tham số khác khi sử dụng rsync:
--progress Hiển thị dung lượng và thời gian còn lại trong quá trình truyền.
--delete nếu file/folder có tồn tại ở thư mục đích nhưng không có ở thư mục nguồn thì xoá ở thư mục đích.
--max-size='200k' Giới hạn chỉ truyền file dung lượng tối đa là 200k (thích 200k thành gì thì sửa)
--remove-source-files Truyền xong thì xoá thư mục/file cũ
--bwlimit=100 Giới hạn băng thông truyền tải.

Cấu hình firewalld và ipset (tạo country blacklist cho firewalld)

Case: bọn trung quốc scan nhiều quá, nên mong muốn block hết IP từ trung quốc lại. Với các hệ thống chuyên nghiệp, FW đời mới hiện nay có thể làm một cách dễ dàng với tính năng GeoIP. Vậy còn các webmaster cá nhân, đơn vị nhỏ chỉ với 1 server public ra ngoài? Họ có thể lựa chọn các giải pháp của cloudflare, akamai, .v. Hoặc cây nhà lá vườn hơn là tận dụng firewalld của centos 7 để làm việc này. Dưới dây là hướng dẫn cách thực hiện:

Bước 1: tạo blacklist cho firewalld
firewall-cmd --permanent --new-ipset=blacklist --type=hash:net --option=family=inet --option=hashsize=4096 --option=maxelem=200000


–permanent : lưu cấu hình vĩnh viễn vào firewalld, nếu không có, sau khi khởi động lại firewall thì lệnh này sẽ mất hiệu lực
–new-ipset : tên của danh sách IP, ở đây chúng ta đặt là blacklist cho dễ quản lý, k thích có thể dùng tên khác như là danhsachden chẳng hạn
–type : kiểu lưu trữ, net dành cho subnet, nếu là 1 IP đơn lẻ thì dùng ip (–type=hash:ip)
–option=family : IPv4 or IPv6 network, inet is for IPv4
–option=hashsize : the initial hash size of the list
–option=maxelem : Số lượng phần tử tối đa của list, ở đây để 200k

Bước 2: tiếp theo chúng ta cần danh sách cái subnet tương ứng với đám IP cần block, ở đây là danh sách các subnet tương ứng với vùng Trung Quốc (CN)_
May thay, trên mạng có sẵn rồi, ở trang ipdeny có đủ toàn bộ thông tin này cho chúng ta. Download về mà xài thôi
wget http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz
tar -vxzf all-zones.tar.gz

cd vào thư mục vừa giải nén, sẽ thấy cả loạt các file text .zone tương ứng với từng quốc gia. Add file của Trung Quốc vào list đã tạo ở trên:
firewall-cmd --permanent --ipset=blacklist --add-entries-from-file=./cn.zone
Chỗ đường dẫn file cn.zone nhớ sửa cho đúng, mình đang đứng trong folder chứa nó nên chỉ là ./ thôi

Bước 3: Chuyển cái list đã add IP ở 2 bước trên sang vùng drop của firewalld
firewall-cmd --permanent --zone=drop --add-source=ipset:blacklist
firewall-cmd --reload

Như vậy, IP source tương ứng với ipset:blacklist sẽ bị firwalld drop 1 cách tự động.
Muốn thêm vùng cần chặn vào thì chỉ cần cho các subnet vào file text theo format của đám .zone đã download ở trên. sau đó
firewall-cmd --permanent --ipset=blacklist --add-entries-from-file=đường dẫn file
Muốn remove 1 số ông khỏi cái list đen đó, thì cũng chỉ add hết subnet cần remove vào 1 file text, ví dụ /tmp/remove_ip.txt
Sau đó chạy:
firewall-cmd --permanent --ipset=test --remove-entries-from-file=/tmp/remove_ip.txt
firewall-cmd --reload

—–

Trick:
Đôi khi cần chặn gấp 1 subnet nào đó. Ngoài cách dùng file như trên, có thể sử dụng script:
firewall-cmd --permanent --ipset=blacklist --add-entry=$1
firewall-cmd --ipset=blacklist --add-entry=$1
firewall-cmd --reload

Lưu lại dưới tên /bin/ban (nhớ chmod +x cho nó nhé)
Mỗi lần cần chặn ai đó, ví dụ chặn khẩn cấp thằng 10.0.5.0/24
Chỉ cần gõ lệnh:
ban 10.0.5.0/24
Nhanh và gọn
Chúc các bạn thành công! (k thành công cũng thành nhân)

Mount google drive, onedrive vào folder của linux

Case study: Cần giải pháp backup file rẻ tiền cho cái blog devopsvn.xyz

Hàng ngày source code và database được dump ra file à đồng bộ file đó lên google drive

Tiến hành:

Để mount folder sử dụng rclone

curl https://rclone.org/install.sh | sudo bash

  • Cấu hình với goole drive: cần chuẩn bị các thông tin dưới đây:
    • https://developers.google.com/identity/sign-in/web/sign-in?refresh=1 truy cập trang này và tạo 1 project, sau đó làm theo hướng dẫn. sẽ thu được client id và client_secret
      • client_secret
      • client_id
    • Trong quá trình chạy thử sẽ thấy không đăng nhập được, trên màn hình console sẽ hướng dẫn truy cập các đường link để xác thực và cấp quyền. Lưu ý theo dõi

rclone config
No remotes found - make a new one
n) New remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
n/r/c/s/q> n
name> remote
Type of storage to configure.
Choose a number from below, or type in your own value
[snip]
10 / Google Drive
\ "drive"
[snip]
Storage> drive
Google Application Client Id - leave blank normally.
client_id>
Google Application Client Secret - leave blank normally.
client_secret>
Scope that rclone should use when requesting access from drive.
Choose a number from below, or type in your own value
1 / Full access all files, excluding Application Data Folder.
\ "drive"
2 / Read-only access to file metadata and file contents.
\ "drive.readonly"
/ Access to files created by rclone only.
3 | These are visible in the drive website.
| File authorization is revoked when the user deauthorizes the app.
\ "drive.file"
/ Allows read and write access to the Application Data folder.
4 | This is not visible in the drive website.
\ "drive.appfolder"
/ Allows read-only access to file metadata but
5 | does not allow any access to read or download file content.
\ "drive.metadata.readonly"
scope> 1
ID of the root folder - leave blank normally. Fill in to access "Computers" folders. (see docs).
root_folder_id>
Service Account Credentials JSON file path - needed only if you want use SA instead of interactive login.
service_account_file>
Remote config
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine or Y didn't work
y) Yes
n) No
y/n> y
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth
Log in and authorize rclone for access
Waiting for code...
Got code
Configure this as a team drive?
y) Yes
n) No
y/n> n
--------------------
[remote]
client_id =
client_secret =
scope = drive
root_folder_id =
service_account_file =
token = {"access_token":"XXX","token_type":"Bearer","refresh_token":"XXX","expiry":"2014-03-16T13:57:58.955387075Z"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

Chú ý: trong quá trình config có thông tin scope, ở trên thông tin này là drive

Thông tin này sử dụng để thao tác sau này:

Cú pháp mount

rclone mount <scope>:<đường dẫn trên google drive> <đường dẫn thư mục local> --daemon
					

ví dụ

rclone mount  homthucuatao1:backup-rclone/ /opt/homthucuatao1/ --daemon

Tham số –daemon thể hiện tiến trình mount chạy ẩn dưới background.    

Kill zombie process linux

[root@abc-kvm93 123_redmine_alert]# ps aux | grep phantom

root 23858 0.1 0.0 0 0 pts/2 Z 11:09 0:01 [phantomjs] <defunct>

root 25464 0.1 0.0 0 0 pts/2 Z 11:16 0:01 [phantomjs] <defunct>

root 27425 0.2 0.0 0 0 pts/2 Z 11:24 0:01 [phantomjs] <defunct>

root 29398 4.4 0.3 2640400 53688 pts/2 Sl 11:32 0:01 /home/huybk/chip1stop_redmine_alert/phantomjs –cookies-file=/tmp/tmp48bddbr7 –webdriver=37998

root 29434 6.4 0.3 2640440 53456 pts/2 Sl 11:32 0:01 /home/huybk/chip1stop_redmine_alert/phantomjs –cookies-file=/tmp/tmpi3guirm8 –webdriver=53345

root 29475 17.7 0.3 2641020 53296 pts/2 Sl 11:32 0:01 /home/huybk/chip1stop_redmine_alert/phantomjs –cookies-file=/tmp/tmprgkv3jhr –webdriver=53106

root 29520 0.0 0.0 103332 876 pts/3 S+ 11:32 0:00 grep phantom

Các process defunct luôn ko mất, gọi là zombie. Do các process này đã ngừng xử lý nhưng vẫn không biến mất khỏi process entry.

Để kill các process này cần giết tiến trình bố của nó. Tìm thằng bố bằng cách chạy
ps -xal

Trường thứ 4 chính là PID của thằng bố nó, kill -9 là xong


Notice: ob_end_flush(): failed to send buffer of zlib output compression (0) in /home/devopsvn/public_html/wp-includes/functions.php on line 5427