Summary of operation and maintenance toolbox

Posted on

summary

This article mainly collects some Linux operation and maintenance tools collected and sorted out since personal work, which are used to investigate, locate and handle various problems, including analyzing network connection, finding out processes that consume high memory, etc

Shell scripting tool

Double and single quotes

Single quotation marks belong to strong quotation, which ignores the special treatment of all the quoted characters, and the quoted characters will be used intact;

Double quotation marks are weak references. They will handle some enclosed characters specially, including the following situations:

  • $plus variable name can take the value of the variable, such as:$PWD
  • The characters enclosed by back quotation marks and $() are replaced by the original characters after the command is executed, such asecho "$(echo hello world)"
  • When characters are needed($ " )In other words, it is necessary to add in the front;

Script full path

  1. Returns the full path, including the file name:FILEPATH="$(readlink -f $0)"
  2. Get the path of the directory where the script is located:BASEDIR="$(dirname $(readlink -f $0))"

environment variable

  1. Set variable, only valid for current process:ENV=debug
  2. Set the environment variable, which can be used by the child processinherit : export ENV=debug

Sed & awk – string text operation

  1. Replacement text:Sed's / original string / replacement string / '
  2. SED and awk use external variables:

    X = mm sed's / AB / '$X' / g 'filename or sed's / AB /' "$X '' / g 'filename sed's /'" $Val '//' filename awk '{print "' $X '}' filename a = 2; b = 3; awk - V A = $a - v b = $B 'begin {printf ("% d% D, N, a, b);}'

Floating point calculation

  1. Using BC:

    $ a=2;b=3;echo "scale=2; $a / $b" | bc .66 $ a=2;b=3;echo "scale=2; $a / $b" | bc -l .66
  2. Using awk:

    $ a=2;b=3;awk 'BEGIN{printf("%.2fn","'$a'"/"'$b'");}' 0.67

String operation

  1. Remove leading and trailing spaces:

    line=" 123 " echo "A${line}B" line=`echo "$line" | xargs` echo "A${line}B"
  2. Case conversion:

    A = ABC "echo $a a a = ${a,}" echo $a  is converted to lowercase
    A = ABC "echo $a a a = ${a ^}" echo $a  is converted to uppercase
  3. Case insensitive comparison:

    A1="ab" A2="Ab" shopt -s nocasematch [[ "$A1" = "$A2" ]];echo $? shopt -u nocasematch
    #Or less
    A1="ab" A2="Ab" test "${A1,,}" = "${A2,,}";echo $?
    #Or less
    A1="ab" A2="Ab" [[ "${A1,,}" = "${A2,,}" ]];echo $?
  4. Take string length:

    ~> str="abcdef" ~> strlen=${#str} ~> echo $strlen 6
  5. Traverse each character of the string:

    ~> str="abcdef" ~> strlen=${#str} ~> echo $strlen 6 ~> for ((i=0; i<$strlen; ++i)) do echo ${str:$i:1}; done a b c d e f
  6. To determine whether a string is a number:

    str1="123abc" str2="123000" $(expr $str1 + 0 > /dev/null 2>&1) echo $? $(expr $str2 + 0 > /dev/null 2>&1) echo $?

Find operation

  1. lookuporRelationship:

    ps aux|grep -E 'abc|xyz'
  2. lookupAndRelationship:

    ps aux|grep "abc.*xyz"
    #Or
    ps aux | grep "abc" | grep "xyz"

Find process

  1. To view all running processes:ps aux
  2. Lists all running processes, including the complete command string:ps auxww
  3. Search for processes that match the string:ps aux | grep STRING
  4. List all processes of the current user in an extra full format:ps --user $(id -u) -F
  5. List all processes of the current user in a tree form:ps --user $(id -u) f
  6. Get the parent node PID of a process:ps -o ppid= -p pid

awk

  • Get IP and port number:

    node="127.0.0.1:2019" eval $(echo "$node" | awk -F[ :,;t]+ '{ printf("ip=%snport=%sn",$1,$2); }') echo $ip echo $port
  • To obtain the IP address on the specified network card:

    ethX=eth1 echo $ethX ip=`netstat -ie|awk -F'[ :]+' 'BEGIN{ok=0;} {if (match($0, "'$ethX'")) ok=1; if ((1ok) && match($0,"inet")) { ok=0; if (7NF) printf("%sn",$3); else printf("%sn",$4); } }'` echo $ip

Scroll log rotate

Logrotate, a log scrolling tool in Linux system, consists of two parts: one is the command line toollogrotateThe second is background servicersyslogd

With rsyslogd, log scrolling can be realized with simple configuration. The configuration file of rsyslogd is/etc/logrotate.confHowever, direct modification is not recommended logrotate.conf , but in the table of contents/etc/logrotate.dHow to add new files.

logrotate.conf All the files in the logrotate. D directory are included. The syntax is consistent, but the difference is that logrotate.conf The default configuration is defined, while the logrotate. D directory is a proprietary configuration.

The following is the demo for configuring log scrolling:

# cat /etc/logrotate.d/redis /usr/local/redis/log/redis-6379.log /usr/local/redis/log/redis-6380.log /usr/local/redis/log/redis-6381.log { rotate 2 minsize 100M nocompress missingok create 0664 redis redis notifempty }

Equipment tools

  • To view the network card model:lspci | grep -i ethernet
  • To view all hardware information:dmidecode
  • View CPU:lscpu
  • To view the motherboard:lscpi
  • To view a SCSI card:lsscsi

System tools

systemctl

  1. system management

    • Shutdown:systemctl poweroff
    • Restart:systemctl reboot
    • Enter single user mode:systemctl rescue
    • To view the system startup time:systemd-analyze
    • List current login users:loginctl list-users
    • To view the status of the user’s root:loginctl user-status root
  2. To view the system status:systemctl status
  3. Snap in:

    • View service unit:systemctl status mysql.service
    • To view unit dependencies:systemctl list-dependencies crond.service

journalctl

staysystemd Before its emergence, the logs of Linux system and its applications were managed separately. SYSTEMd began to manage the startup logs of all units in a unified way. In this way, you can view the logs of all kernel and applications with only one journalctl command

  • View log:

    • To view all logs, including kernel and application logs:journalctl
    • Only view the kernel log, output in JSON format, and not by page:journalctl -k -o json --no-pager
    • To view the system startup log:journalctl -b
    • To view the disk size occupied by the log:jourctl --disk-usage
  • To view the log at a specified time:

    journalctl --since "10 min ago" journalctl --since="2019-11-22 19:50:22" journalctl --since yesterday journalctl --since "2019-11-20" --until "2019-11-21 10:00"
  • To view the specified process log:

    journalctl _PID=`pidof crond`
  • To view the specified user log:

    journalctl _UID=`id -r -u root` --since today

Restart the service

Different distributions of Linux may have different restart methods. Take nginx as an example:

  1. service nginx restart
  2. sysctmctl restart nginx

Service is the old restart service mode, while systemctl is the new one. Service is a script, while systemctl is an executable program.

SAR – monitors the performance of different Linux subsystems

Full nameSystem Activity ReporterSystem activity report, one of the most comprehensive system performance analysis tools, can also be used to view network traffic.

Vmstat – system level memory monitoring tool

Reports information about processes, memory, paging, block IO, traps, disk, and CPU activity.

  1. Display virtual memory statistics:vmstat
  2. The report is displayed 5 times every 2 seconds:vmstat 2 5

Iostat – system level disk IO monitoring tool

  1. Displays the CPU and disk statistics report since the system started:iostat
  2. Display CPU and disk statistics report, unit conversion to megabytes:iostat -m
  3. Extended disk statistics showing the disk name of device “SDA”:iostat -xN sda

Iotop – process level disk IO monitoring tool

Mpstat – reports CPU statistics

  1. Display CPU statistics every 2 seconds:mpstat 2
  2. Display 5 reports one by one from a given processor at 2 second intervals:mpstat -P 0 2 5

View system interrupts

cat /proc/interrupts

View network card interrupt

grep eth0 /proc/interrupts |awk '{print $NF}'

View interrupt affinity

cat /proc/irq/74/smp_ Affinity ා take interrupt 74 as an example

Lsof – lists open files and corresponding processes

  1. Find the process that opened the local port:lsof -i :PORT
  2. Find the process that opens the given file:lsof /path/to/file
  3. Output process ID (PID) only:lsof -t /path/to/file
  4. Lists the files opened by the specified user:lsof -u username
  5. Lists the files opened by the specified command or process:lsof -c process_or_command_name
  6. Lists the files opened by a specific process and gives its PID:lsof -p PID
  7. List the open files in the directory:lsof +D path/to/directory
  8. Found process listening on local TCP port:lsof -iTCP:port -sTCP:LISTEN

Fuser – displays the process ID of the file or socket currently in use

  1. To view the processes holding TCP 2019 ports:fuser -v -n tcp 2019
  2. Kill all processes that open the file / TMP / test:fuser -m -k /tmp/test
  3. View the file / TMP / test opened by those processes:fuser -um /tmp/test

Free – displays the free and used memory in the system

Screen, byobu and TMUX – split screen tools

Dmesg – writes kernel information to standard output

Multitail – Tai like tool for monitoring multiple documents at the same time

Process tools

Pwdx – the working directory of the print process

Pidof – view process ID

Nice and ionice prioritization tools

Nice is the CPU priority view and adjustment tool of the process, and ionice is the IO priority view and adjustment tool of the process.

Pstack – view call stack

Performance tools

Valgrind and qcachegrind memory analysis tools

Open source memory analysis and performance analysis tools. Qcachegrind is a Valgrind assistant tool, which can visually view the results of Valgrind performance analysis.

Perf – Linux performance analysis tool

Linux comes with powerful performance analysis tools, which can be combined with flame chart. How to use it, such as:perf top -p pid。 Timechart, a graphical tool for generating SVG format, can also be combined withFlameGraphGenerate a flame diagram.

Record process performance statistics for a period of time:perf record -p pid -g -e event -o logfile perf report -i logfile

AB, Tsung, siege – stress testing tools

Network tools

Netstat and SS

SS is a socket statistics tool that can replace netstat.

  1. To view TCP listening:netstat -lpnt
  2. To view TCP connections:netstat -lpna

Ifconfig and IP

  • One IP setting:ip addr add 192.168.31.13/24 dev eth1
  • Check whether the set IP is effective:ip addr show eth1
  • Delete IP:ip addr del 192.168.0.121/24 dev eth1
  • To view the routing table:ip route show

Tcpdump – network traffic monitoring

  • List the available network interfaces:tcpdump -D
  • Capture traffic for a specific interface:tcpdump -i eth0
  • Capture all TCP traffic and display the content (ASCII) on the console:tcpdump -A tcp
  • Capture traffic from or to the host:tcpdump host www.example.com
  • Destination, source, and capture:tcpdump -i eth0 src 192.168.1.1 and dst 192.168.1.2 and dst port 80
  • Capture network traffic:tcpdump net 192.168.1.0/24
  • Capture all traffic except port 22 and save to a file:tcpdump -w dumpfile.pcap not port 22
  • Read traffic data from file:tcpdump -r dumpfile.pcap

Iftop – network bandwidth monitoring

Socat – multifunctional network tool

Full nameSocket CAT , is an enhanced version of netcat

/Proc file system

  • /Proc / meminfo: memory size and usage information
  • /Proc / cpuinfo: number and frequency of CPUs
  • /Proc / PID and / proc / PID / maps: various information of the process, in which PID is the process ID. if the process ID is 2019, then the path is “/ proc / 2019”. The file descriptors created and opened by a process are all in the/proc/PID/fdNext, take the init process of Linux as an example

    ls /proc/1/fd 0 1 10 11 12 13 14 15 16 17 2 20 21 22 24 25 26 27 28 29 3 30 31 32 33 34 37 38 39 4 5 6 7 8 9
  • /proc/irq

    1. /Proc / IRQ /: this directory stores the directory named after the IRQ number. For example, / proc / IRQ / 40 / indicates the relevant information of interrupt number 40
    2. /proc/irq/[irq_ num]/smp_ Affinity: this file stores the CPU bitmask (hexadecimal). Modifying the value in the file can change the affinity between CPU and an interrupt
    3. /proc/irq/[irq_ num]/smp_ affinity_ List: this file stores the CPU list (decimal). Note that the number of CPU cores starts from 0, such as cpu0 and CPU1
  • /proc/net

    1. /Proc / net / dev: Statistics network card traffic
    2. /Proc / net / sockstat: various states of socket
  • /Proc / sys / Fs file system correlation

    1. /proc/sys/fs/file-max
    2. /proc/sys/fs/file-nr
    3. /proc/sys/fs/inode-nr
  • /Proc / sys / net: network related

    1. /Proc / sys / net / core / somaxconn controls the size of TCP listening queue
    2. /proc/sys/net/ipv4/tcp_ fin_ Timeout control fin_ WAIT_ 2 the timeout duration of the state
    3. /proc/sys/net/ipv4/tcp_keepalive_intvl
  • /Proc / sys / VM: memory dependent

    1. /proc/sys/vm/drop_caches
    2. /proc/sys/vm/overcommit_memory

MISC

History – command line history

View Linux distributions

cat /etc/*-release

Get machine IP address

netstat -ie|awk /broadcast/'{print $2}' netstat -ie|awk -F '[ :]+' /cast/'{print $4}' netstat -ie|awk -F '[ :]+' /cast/'{print $3}'

Clear sys

Leave a Reply

Your email address will not be published.