还没想好用什么标题

0%

这周发生好几件大事:

  1. 谷歌发布SHA-1安全加密碰撞实例
  2. Cloudflare 泄露网络会话中的加密数据
  3. linux内核漏洞 CVE-2017-6074

加密在网络中越来越受关注,目前github的提交仍然是以SHA-1作为标签的,期待后期改善。

下面是安装使用git的简短记录, 有些翻译不完整

安装

如果需要 Bash 命令补完(也即按下 Tab 来完成你正在键入的命令),请在~/.bashrc文件中添加如下内容:

1
source /usr/share/git/completion/git-completion.bash

你也可以安装 bash-completion 来自动为 shell 提供命令补完。

如果你想使用 Git 内建的图形界面(例如 gitk 或者 git gui),你需要安装 tk 软件包,否则你会遇到一个隐晦的错误信息:

1
/usr/bin/gitk: line 3: exec: wish: not found.

配置

Git 从若干 INI 格式的配置文件中读取配置信息。在每一个 git 版本库中,.git/config 用于指定与该版本库有关的配置选项。在 $HOME/.gitconfig 中的用户 (“global”) 的配置文件将被用作仓库配置的备用配置。你可以直接编辑配置文件,但是更推荐的方法是使用 git-config 工具。例如,

1
$ git config --global core.editor "nano -w"

会在 ~/.gitconfig 文件的 [core] 部分中添加 editor = nano -w。

git-config 工具的 man page 提供了完整的选项列表。

这是一些你可能用到的常见的配置:

1
2
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "[email protected]"

在 Git 命令行下启用彩色输出
配置 color.ui 选项可以令 Git 以彩色输出信息。

1
$ git config --global color.ui true

解决 Git 在命令行下中文文件名显示为数字的问题

1
$ git config --global core.quotepath false

基本用法

克隆一个版本库

以下命令可以将一个 Git 版本库克隆至本地目录的新文件夹中:

1
git clone <repo location> <dir>

如果留空

字段,就会以 Git 版本库的名称命名新文件夹,例如:

1
git clone [email protected]:torvalds/linux.git

可以将 GitHub 上 Linux 内核的镜像克隆至名为「linux」的文件夹中。

提交(commit)文件到版本库
Git 的提交过程分为两步:
添加新文件、修改现有的文件(均可通过 git add 完成), 或者删除文件(通过 git rm 完成)。这些修改将被存入名叫 index 的文件中。

使用 git commit 提交修改。
Git 提交时会打开文本编辑器,填写提交信息。你可以通过 git config 命令修改 core.editor 来选择编辑器。

此外,你也可以直接用 git commit -m 命令在提交时填写提交信息,这样就不会打开编辑器。

其它有用的技巧:

git commit -a lets you commit changes you have made to files already under Git control without having to take the step of adding the changes to the index. You still have to add new files with git add.
git commit -a 命令可以跳过添加修改的部分,但是如果创建新文件依然需要 git add。
git add -p 命令可以提交修改文件的特定部分。如果你进行了许多修改而且希望将其分多次提交的话,这一选项非常有用。

将改动提交(push)到公共版本库
以下命令可以将修改提交至服务器(例如 Github):

1
git push <server name> <branch>

添加 -u 参数可以将该服务器设为当前分支(branch)提交时的默认服务器。如果你是通过上文的方法克隆的版本库,默认服务器将是你克隆的来源(别名「origin」),默认分支将是 master。也就是说如果你按照上文的方法克隆的话,提交时只要执行 git push 即可。如果需要的话,可以令 Git 提交至多个服务器,不过这比较复杂。下文将讲解分支(branch)。

从服务器公共版本库下载修改
如果你在多台电脑上工作,并且需要将本地版本库与服务器更新,可以执行:

1
git pull <server name> <branch>

与 push 类似,server name 与 branch 都可以根据默认来,所以只需执行 git pull。
Git pull 实际上是如下两个命令的简写:

git fetch,将服务器文件复制至本地,这一分支被称作「remote」也即它是远程服务器的镜像。
git merge,将「remote」分支的文件与本地文件合并。如果你的本地提交记录与服务器的提交记录相同,就可以直接得到服务器的最新版本。如果你的提交记录与服务器的记录不符(例如在你最后一次提交之后别人进行了提交),两份提交记录将被合并。

It is not a bad idea to get into the practice of using these two commands instead of git pull. This way you can check to make sure that the server contains what you would expect before merging.
分步执行两个命令而非 git pull 并不是坏事,这样可以确保合并之前服务器的文件与你期望的相同。

查看历史记录
git log 命令可以显示当前分支的历史记录。注意每一次提交(commit)会以一个 SHA-1 标记区分,接下来是提交者、提交日期以及提交信息。更实用的命令:

1
git log --graph --oneline --decorate

可以显示与 TortoiseGit 的提交记录类似的窗口,这一窗口包含了如下内容:
每次提交的 SHA-1 标记的前七位(足以区分不同的提交)
–graph 选项可以显示从当前分支 fork 的分支数目(如果有的话)
–oneline 选项可以在一行内显示每次提交的信息
–decorate 选项可以显示所有的提交信息(包括分支与标签)

可以通过如下命令将这一命令以 git graph 的别名保存:

1
git config --global alias.graph 'log --graph --oneline --decorate'

现在执行 git graph 将等价于执行 git log –graph –oneline –decorate。
git graph 与 git log 命令也可以带 –all 的参数执行,这将显示所有的分支信息,而不止当前的分支。
也可以带 –stat 参数执行,它可以显示每次提交时哪些文件有修改、修改了多少行。

处理合并(merge)
当你执行 pull、进行复原操作,或者将一个分支与另一个进行合并时会需要处理合并。与其它 VCS 类似,当 Git 无法自动处理合并时,就需要使用者进行处理。
可以查看 Git Book 的这一部分讲解如何处理冲突合并。
如果你需要通过合并来还原的话,可以带 –abort 参数运行合并相关的命令,例如 git merge –abort,git pull –abort,git rebase –abort)。

使用分布式版本控制系统

The above commands only provide the basics. The real power and convenience in Git (and other distributed version control systems) come from leveraging its local commits and fast branching. A typical Git workflow looks like this:
Create and check out a branch to add a feature.
Make as many commits as you would like on that branch while developing that feature.
Squash, rearrange, and edit your commits until you are satisfied with the commits enough to push them to the central server and make them public.
Merge your branch back into the main branch.
Delete your branch, if you desire.
Push your changes to the central server.

创建一个分支

1
git branch <branch name>

can be used to create a branch that will branch off the current commit. After it has been created, you should switch to it using

1
git checkout <branch name>

A simpler method is to do both in one step with

1
git checkout -b <branch name>

To see a list of branches, and which branch is currently checked out, use

1
git branch

A word on commits
Many of the following commands take commits as arguments. A commit can be identified by any of the following:
Its 40-digit SHA-1 hash (the first 7 digits are usually sufficient to identify it uniquely)
Any commit label such as a branch or tag name
The label HEAD always refers to the currently checked-out commit (usually the head of the branch, unless you used git checkout to jump back in history to an old commit)
Any of the above plus ~ to refer to previous commits. For example, HEAD~ refers to one commit before HEAD and HEAD~5 refers to five commits before HEAD.

提交为检查点
In Subversion and other older, centralized version control systems, commits are permanent - once you make them, they are there on the server for everyone to see. In Git, your commits are local and you can combine, rearrange, and edit them before pushing them to the server. This gives you more flexibility and lets you use commits as checkpoints. Commit early and commit often.

编辑之前的提交

1
git commit --amend

allows you to modify the previous commit. The contents of the index will be applied to it, allowing you to add more files or changes you forgot to put in. You can also use it to edit the commit message, if you would like.

插入、重新排序和更改历史记录

1
git rebase -i <commit>

will bring up a list of all commits between and the present, including HEAD but excluding . This command allows you rewrite history. To the left of each commit, a command is specified. Your options are as follows:
The “pick” command (the default) uses that commit in the rewritten history.
The “reword” command lets you change a commit message without changing the commit’s contents.
The “edit” command will cause Git to pause during the history rewrite at this commit. You can then modify it with git commit –amend or insert new commits.
The “squash” command will cause a commit to be folded into the previous one. You will be prompted to enter a message for the combined commit.
The “fixup” command works like squash, but discards the message of the commit being squashed instead of prompting for a new message.
Commits can be erased from history by deleting them from the list of commits
Commits can be re-ordered by re-ordering them in the list. When you are done modifying the list, Git will prompt you to resolve any resulting merge problems (after doing so, continue rebasing with git rebase –continue)
When you are done modifying the list, Git will perform the desired actions. If Git stops at a commit (due to merge conflicts caused by re-ordering the commits or due to the “edit” command), use git rebase –continue to resume. You can always back out of the rebase operation with git rebase –abort.
Warning: Only use git rebase -i on local commits that have not yet been pushed to anybody else. Modifying commits that are on the central server will cause merge problems for obvious reasons.
Note: Vim makes these rebase operations very simple since lines can be cut and pasted with few keystrokes.

Git提示符
The Git package comes with a prompt script. To enable the prompt addition you will need to source the git-prompt.sh script and add $(__git_ps1 “ (%s)”) to you PS1 variable.
Copy /usr/share/git/completion/git-prompt.sh to your home directory (e.g. ~/.git-prompt.sh).
Add the following line to your .bashrc/.zshrc:
source ~/.git-prompt.sh
For Bash:

1
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

Note: For information about coloring your bash prompt see Color_Bash_Prompt
For zsh:

1
PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

The %s is replaced by the current branch name. The git information is displayed only if you are navigating in a git repository. You can enable extra information by setting and exporting certain variables to a non-empty value as shown in the following table:

TODO: 表格格式化

1
2
3
4
Variable	Information
GIT_PS1_SHOWDIRTYSTATE * for unstaged and + for staged changes
GIT_PS1_SHOWSTASHSTATE $ if something is stashed
GIT_PS1_SHOWUNTRACKEDFILES % if there are untracked files

传输协议

智能HTTP

Since version 1.6.6 git is able to use the HTTP(S) protocol as efficiently as SSH or Git by utilizing the git-http-backend. Furthermore it is not only possible to clone or pull from repositories, but also to push into repositories over HTTP(S).
The setup for this is rather simple as all you need to have installed is the Apache web server (with mod_cgi, mod_alias, and mod_env enabled) and of course, git:

Once you have your basic setup up and running, add the following to your Apache’s config usually located at /etc/httpd/conf/httpd.conf:

1
2
3
4
<Directory "/usr/lib/git-core*">
Order allow,deny
Allow from all
</Directory>
1
2
3
SetEnv GIT_PROJECT_ROOT /srv/git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/

The above example config assumes that your git repositories are located at /srv/git and that you want to access them via something like http(s)://your_address.tld/git/your_repo.git. Feel free to customize this to your needs.
Note: Of course you have to make sure that your Apache can read and write (if you want to enable push access) on your git repositories.
For more detailed documentation, visit the following links:
http://progit.org/2010/03/04/smart-http.html
https://www.kernel.org/pub/software/scm/git/docs/v1.7.10.1/git-http-backend.html

Git SSH
You first need to have a public SSH key. For that follow the guide at Using SSH Keys. To set up SSH itself, you need to follow the SSH guide. This assumes you have a public SSH key now and that your SSH is working. Open your SSH key in your favorite editor (default public key name is ~/.ssh/id_rsa.pub), and copy its content (Ctrl+c). Now go to your user where you have made your Git repository, since we now need to allow that SSH key to log in on that user to access the Git repository. Open ~/.ssh/authorized_keys in your favorite editor, and paste the contents of id_rsa.pub in it. Be sure it is all on one line! That is important! It should look somewhat like this:
Warning: Do not copy the line below! It is an example! It will not work if you use that line!

1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCboOH6AotCh4OcwJgsB4AtXzDo9Gzhl+BAHuEvnDRHNSYIURqGN4CrP+b5Bx/iLrRFOBv58TcZz1jyJ2PaGwT74kvVOe9JCCdgw4nSMBV44cy+6cTJiv6f1tw8pHRS2H6nHC9SCSAWkMX4rpiSQ0wkhjug+GtBWOXDaotIzrFwLw== username@hostname

Now you can checkout your Git repository this way (change where needed. Here it is using the git username and localhost):
git clone git@localhost:my_repository.git
You should now get an SSH yes/no question. Type yes followed by Enter. Then you should have your repository checked out. Because this is with SSH, you also do have commit rights now. For that look at Git and Super Quick Git Guide.

特定非标准端口
Connecting on a port other than 22 can be configured on a per-host basis in /etc/ssh/ssh_config or ~/.ssh/config. To set up ports for a repository, specify the path in .git/config using the port number N and the absolute path /PATH/TO/REPO:

1
ssh://[email protected]:N/PATH/TO/REPO

Typically the repository resides in the home directory of the user which allows you to use tilde-expansion. Thus to connect on port N=443,

1
url = [email protected]:repo.git

becomes:

1
url = ssh://[email protected]:443/~git/repo.git

Git守护进程
Note: The git daemon only allows read access. For write access see #Git SSH.
This will allow URLs like “git clone git://localhost/my_repository.git”.
Edit the configuration file for git-daemon /etc/conf.d/git-daemon.conf (GIT_REPO is a place with your git projects), then start git-daemon with root privileges:

1
# systemctl start git-daemon@

To run the git-daemon every time at boot, enable the service:

1
# systemctl enable git-daemon@

Clients can now simply use:

1
git clone git://localhost/my_repository.git

Git版本库权限
To restrict read/write access, you can simply use Unix rights, see http://sitaramc.github.com/gitolite/doc/overkill.html
For a fine-grained rights access, see gitolite and gitosis

编辑

简介

Nginx (“engine x”) 是一个轻量级,高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名,其特点是占有内存少,并发能力强。

一个nginx.conf例子

这是官网上的一个配置,参照该配置,可以初步一窥nginx设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
user  www www;     # 运行nginx的用户及用户组
worker_processes 2; #启动的进程数
pid /var/run/nginx.pid; #pid文件位置

# [ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx.error_log info; #日志存放及日志等级设置

events {
worker_connections 2000; #每个进程最大的连接数 默认1024
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
use kqueue; #使用的处理机制 epoll可以容纳更多请求
}

http {
include conf/mime.types; # 加载mime
default_type application/octet-stream; #默认文件类型

log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"'; #设置日志格式

log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';

client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;

client_header_buffer_size 1k;
large_client_header_buffers 4 4k;

gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain; #设置压缩

output_buffers 1 32k;
postpone_output 1460;

sendfile on;
tcp_nopush on;

tcp_nodelay on;
send_lowat 12000;

keepalive_timeout 75 20;

# lingering_time 30;
# lingering_timeout 10;
# reset_timedout_connection on;


server { #server段
listen one.example.com;
server_name one.example.com www.one.example.com;

access_log /var/log/nginx.access_log main;

location / { #location段
proxy_pass http://127.0.0.1/; #设置代理
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;
client_body_buffer_size 128k;

client_body_temp_path /var/nginx/client_body_temp;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_send_lowat 12000;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_temp_path /var/nginx/proxy_temp;

charset koi8-r;
}

error_page 404 /404.html; #定义404页面

location /404.html {
root /spool/www;

charset on;
source_charset koi8-r;
}

location /old_stuff/ {
rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent; #rewrite重定向
}

location /download/ {
valid_referers none blocked server_names *.example.com;

if ($invalid_referer) { #if判断条件
#rewrite ^/ http://www.example.com/;
return 403;
}

# rewrite_log on;
# rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3
rewrite ^/(download/.*)/mp3/(.*)\..*$ /$1/mp3/$2.mp3 break;

root /spool/www;
# autoindex on;
access_log /var/log/nginx-download.access_log download;
}

location ~* ^.+\.(jpg|jpeg|gif)$ { #为静态资源设置缓存
root /spool/www;
access_log off;
expires 30d;
}
}
}

负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
http {
upstream myproject {
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}

server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://myproject;
}
}
}

反向代理及缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
http {
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m
inactive=24h max_size=1g;
server {
location / {
proxy_pass http://1.2.3.4;
proxy_set_header Host $host;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
}
}
}

重定向

1
2
3
4
5
6
7
8
http {

server {
listen 80;
server_name www.domain.com;
return 301 https://www.domain.com$request_uri;
}
}

反向代理某G

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 443 ssl http2;
server_name google.domain.com;

root /usr/share/nginx/html;
index index.html index.htm;

ssl on;
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/letsencrypt/dhparams.pem;

location / {
proxy_pass https://www.replace.com/;
}
}

http/2支持

http/2 至少需nginx 1.9版本以上, 编译时openssl版本建议也使用比较高版本不低于 1.0.2

补充链接: https://www.zybuluo.com/phper/note/89391

将可能用到的第三方http请求进行反响代理

1
2
3
4
5
6
7
8
location ~ "^/proxy/(.*)$" {
resolver 8.8.8.8;
proxy_pass http://$1;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
expires 7d;
}

add a fallback to my proxy in nginx

https://serverfault.com/questions/765483/how-to-add-a-fallback-to-my-proxy-in-nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
listen 8080;
server_name mydomain;
access_log /log/path/logging.log;
error_page 400 401 402 403 404 405 500 501 502 503 504 @error_page;

location @error_page {
root /var/www/html/;
rewrite ^ https://domain.com/error/index.html;
break;
}

location / {
proxy_redirect off;
proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_intercept_errors on;

proxy_pass http://127.0.0.1:1337;
}
}

This will redirect all traffic from maindomain:8080 to https://domain.com/error/index.html if the service on http://127.0.0.1:1337 is unavailable(all errors).

sar 是属于sysstat包中的一个工具

安装sysstat包后,默认创建一个/etc/cron.d/sysstat文件,其默认内容为:

1
2
3
4
# run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib/sa/sa1 1 1
# generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib/sa/sa2 -A

这里用到了两个命令/usr/lib/sa/sa1 /usr/lib/sa/sa2

sa1:是调用sadc(二进制文件),将数据收集到二进制日志文件的一个Shell脚本。sa1命令还确保每天使用不同的文件。每隔十分钟运行一次该命令,最好不要改这个值,这是对一般系统折中的值。二进制日志文件存放在/var/log/sa/目录下,命名为sa${DATE}。

sa2:是将当日二进制日志文件中所有的数据转储到文本文件(sar)的另一个Shell脚本,然后它将清除七天之前的所有日志文件。参数-A指定了从二进制文件中提取哪些数据转储到文本文件中。转储的文件存放在/var/log/sa/目录下,命名为sar${DATE}。

这两个命令要配合着使用。

查看一下/usr/lib/sa/sa1脚本,里面执行这样一句命令:

1
/usr/lib/sa/sadc -F -L 1 1 -

-F:强制指定一个储存文件,如果文件已存在,就将其转换成sa的二进制文件形式。
-L:给sa文件加互斥锁,不能让两个sadc进程同时写一个sa文件。

跟据sa1脚本中的命令,我们也可以手动的创建sa二进制文件,使用/usr/lib/sa/sadc命令,

1
# /usr/lib/sa/sadc 1 10 /tmp/jaylin_sa

上述命令的作用是:每隔1s写一条记录,写10条,存放到二进制文件/tmp/jaylin_sa中。

查看一下/usr/lib/sa/sa2脚本,里面执行这样一句命令:

1
/usr/bin/sar -A -f /var/log/sa/sa${DATE} > /var/log/sa/sar${DATE}

-A:列出所有存储在/var/log/sa/sa${DATE}里的统计信息。
-f:指定将要转储的sa文件,默认的参数值为/var/log/sa/sa${DATE}。

根据sa2脚本中的命令,我们也可以手动将sa文件(二进制)转储到sar文件(ASCII文本)中,使用/usr/bin/sar命令,

1
# /usr/bin/sar -A -f /tmp/jaylin_sa> /tmp/jaylin_sar

我们可以通过cat等命令查看sar文件的内容。但是我们也可以通过sar命令读取sa的二进制文件。

1
2
3
4
5
6
7
8
9
# sar
03:00:01 PM CPU %user %nice %system %iowait %steal %idle
03:10:01 PM all 1.02 0.00 2.33 0.39 0.00 96.25
03:20:01 PM all 1.85 0.00 3.29 0.32 0.00 94.54
03:30:02 PM all 1.63 0.06 3.81 2.05 0.00 92.45
03:40:02 PM all 9.31 0.00 8.10 3.39 0.00 79.20
03:50:01 PM all 8.64 0.00 7.73 2.27 0.00 81.36
04:00:01 PM all 0.84 0.00 2.12 1.81 0.00 95.23
Average: all 3.88 0.01 4.56 1.71 0.00 89.84

其中:

%user:CPU花费在用户进程(如应用程序、Shell脚本或该用户进行的交互)上的时间的百分比。
%nice:CPU用来执行有用户级别优先级别的任务的时间的百分比。
%system:CPU用来执行核心任务的时间的百分比。
%iowait:CPU等待块设备输入或输出的时间的百分比。
%steal:CPU等待管理程序(hypervisor)处理其他任务的时间的百分比。
%idle:CPU未进行任何有用操作的时间的百分比。

最后一行是所有数据的平均值。然而,因为大多数系统都会在忙时间段后经历空闲时间段,所以平均值并不能反映完整的情况。

其他度量参数:

-b:显示了缓冲区信息和使用缓冲区与必须写磁盘的比率。
-c:显示了系统调用分解为一些常用的调用,如fork()、exec()、read()和write()。高进程 创建会导致较差的性能,并且这是可能需要将一些应用程序转移到其他计算机的信号。
-p 和 -w:显示了分页(交换)活动。高分页操作是内存缺乏的信号。特别地,-w 选项 显示了进程切换的次数:高的数值表示计算机上运行的内容过多,该计算机在切换任务 上花费了比实际工作更多的时间。
-q:显示了运行队列的大小,它与当时的平均负载相同。
-r:显示了一段时间的可用内存和交换空间。

它们显示每一列的含义请查看sar的man page。

我们也可以查看指定的某一时间段内的记录

-f:指定要读取的sa文件。
-s:开始的时间。注意,-s不是包含性的,所以必须从所选择的开始时间减去十分钟。
-e:结束的时间。

例如:

1
2
3
4
5
# # sar -f /var/log/sa/sa12 -s 14:50:00 -e 15:30:00
03:00:01 PM CPU %user %nice %system %iowait %steal %idle
03:10:01 PM all 1.02 0.00 2.33 0.39 0.00 96.25
03:20:01 PM all 1.85 0.00 3.29 0.32 0.00 94.54
Average: all 1.44 0.00 2.81 0.36 0.00 95.40

上述命令查看本月12日,15:00—15:30之间的记录。

下面介绍一个第三方工具Ksar
ksar这个工具可以将sar文件装换成图形,便于查看内存等数据的变化情况。
ksar这款软件在2009年以后已经不在更新, 由于需要解析阿里云产生的监控数据,以便更好的展示, 我下载安装ksar

以下记录操作过程

  1. 下载5.0.6的zip文件
  2. 解压zip文件
  3. java -jar kSar.jar 运行这个程序

注意,如果是手动将sa文件转储到sar文件中,执行sar命令时要加LANG=C,即:

1
# LANG=C /usr/bin/sar -A -f /tmp/jaylin_sa> /tmp/jaylin_sar

不加LANG=C,sar文件显示的时间为12小时制;加LANG=C之后,sar文件显示的时间为24小时制。Ksar工具只能识别24小时制的sar文件,所以切记执行转储时要加此环境变量。

这里遇到的问题
阿里云上的监控数据是使用sar获取的 以纯文本的形式记录, 但是阿里云的日志上时间是以12小时制的

1
2
3
4
5
6
7
8
9
10
12:00:01 AM     CPU      %usr     %nice      %sys   %iowait    %steal      %irq     %soft    %guest     %idle
12:10:01 AM all 28.75 0.00 0.25 0.02 0.00 0.00 0.05 0.00 70.93
12:10:01 AM 0 42.95 0.00 0.51 0.04 0.00 0.00 0.39 0.00 56.12
12:10:01 AM 1 35.92 0.00 0.35 0.01 0.00 0.00 0.00 0.00 63.72
12:10:01 AM 2 32.93 0.00 0.30 0.02 0.00 0.00 0.00 0.00 66.75
12:10:01 AM 3 30.23 0.00 0.23 0.03 0.00 0.00 0.00 0.00 69.51
12:10:01 AM 4 21.33 0.00 0.19 0.02 0.00 0.00 0.00 0.00 78.47
12:10:01 AM 5 20.28 0.00 0.15 0.01 0.00 0.00 0.00 0.00 79.57
12:10:01 AM 6 20.30 0.00 0.15 0.01 0.00 0.00 0.00 0.00 79.54
12:10:01 AM 7 26.14 0.00 0.11 0.01 0.00 0.00 0.00 0.00 73.74

而ksar无法识别这一格式, 会报这个错误

1
2
3
4
5
6
7
java.lang.NumberFormatException: For input string: "all"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Float.valueOf(Unknown Source)
at java.lang.Float.<init>(Unknown Source)
at net.atomique.ksar.Linux.Parser.parse(Parser.java:624)
at net.atomique.ksar.kSar.parse(kSar.java:750)
at net.atomique.ksar.FileRead.run(FileRead.java:62)

那就转换成24小时制的吧, 第一反应是vim

本来简单的以为这个修改在vim里面应该很容易完成, 不想vim太高冷, 还是驾驭不住. 用vim试了一个多小时无果

只能用shell来处理了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash                                                                                                                                                                                                                                 
#
output="web1"
inpath="/tmp/sar/web1_sa"

change() {
while read line; do
temp1=`echo "$line" |awk '{print $1}'`
temp2=`echo "$line" |awk '{print $2}'`
if [ "$temp2" == "AM" ]; then
newtime=`date -d "$temp1 AM" +%T`
echo "`echo "$line" |sed "s/[0-9][0-9]:[0-9][0-9]:[0-9][0-9] AM/$(echo $newtime)/"`" >> $1_$output
elif [ "$temp2" == "PM" ]; then
newtime=`date -d "$temp1 PM" +%T`
echo "`echo "$line" |sed "s/[0-9][0-9]:[0-9][0-9]:[0-9][0-9] PM/$(echo $newtime)/"`" >> $1_$output
else
echo "$line" >> $1_$output
fi
done < $1
echo >> $1_$output
}

for i in `ls $inpath/sar*`
do
change $i &
done

如果各位有好的vim解决方案, 还请不吝赐教

由于这里采用markdown的编辑器, 而markdown语法还未到信手拈来的地步

最后, 附上markdown语法简图

markdown

kconfig makeconfig 说明

内核源码树的目录下都有两个文档Kconfig(2.4版本是 Config.in)和Makefile。
分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源文档 相关的内核配置菜单。
在内核配置make menuconfig(或xconfig等)时,从Kconfig中读出菜单,用户选择后保存到.config的内核配置文档中。
在内核编译时,主Makefile调用这个.config,就知道了用户的选择。

阅读全文 »

对docker做简单的说明

之后将写一篇关于 btrfs 的
一篇 mongodb 的

再之后如果有空的话 搭建个 mail 服务器 用域名来发邮件 方便交流

有空再搜集资料
patch操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2, patch 操作 
生成patch
leodemac-mini:openwebrtc-examples___stefanalund leo$ git format-patch HEAD^
生成 0001-modify-for-load-leo-s-page.patch

git format-patch -s 1bbe3c8c197a35f79bfddaba099270a2e54ea9c7


或者使用
git format-patch HEAD^ <==最近的1次commit的patch
git format-patch HEAD^^ <==最近的2次commit的patch
git format-patch HEAD^^^<==最近的3次commit的patch
git format-patch HEAD^^^^ <==最近的4次commit的patch
git format-patch HEAD^^^^^ <==不支持!!!!error!!!



使用patch


将补丁打上去。
patch -p1 < 0001-Added-liuxingde-test.patch

打開 patch 文件, 一開始就看得到 path 的路逕所在.

-p[n] 的 n 值, 只要取消多少條 / 及其左邊的路逕.
以 /usr/src/Linux 為例,
若 -p0 就是不取消任何路經
-p1 則將 / 取消, 得 usr/src/linux
-p2 則是將 /usr/ 取消, 得 src/linux
再以 src/linux 為例:
-p0 依然為 src/linux
-p1 則為 linux

后面想写几个命令的笔记

find
awk
sed
grep (正则表达式)
vim
bash (这里面东西太多了,慢慢来吧)
一些系统监控命令