kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

linux下logrotate 配置和理解

1
systemctl restart rsyslog

https://blog.csdn.net/cjwid/article/details/1690101

一、logrotate 配置

logrotate 程序是一个日志文件管理工具。用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做"转储"。我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行。 logrotate 程序还可以用于压缩日志文件,以及发送日志到指定的E-mail 。

logrotate 的配置文件是 /etc/logrotate.conf。主要参数如下表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
参数 功能
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).

二、缺省配置 logrotate

logrotate 缺省的配置 /etc/logrotate.conf。 Red Hat Linux 缺省安装的文件内容是:

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
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# send errors to root
errors root
# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
#compress # 指定不压缩转储文件,如果需要压缩,去掉注释就可以了。

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own lastlog or wtmp --we'll rotate them here
/var/log/wtmp {
	monthly
	create 0664 root utmp
	rotate 1
}

/var/log/lastlog {
	monthly
	rotate 1
}

# system-specific logs may be configured here

三、使用include 选项读取其他配置文件

include 选项允许系统管理员把分散到几个文件的转储信息,集中到一个 主要的配置文件。当 logrotate 从logrotate.conf 读到include 选项时,会从指定文件读入配置信息,就好像他们已经在/etc/logrotate.conf 中一样。

第13行 include /etc/logrotate.d 告诉 logrotate 读入存放在/etc/logrotate.d 目录中的日志转储参数,当系统中安装了RPM 软件包时,使用include 选项十分有用。RPM 软件包的日志转储参数一般存放在/etc/logrotate.d 目录。

include 选项十分重要,一些应用把日志转储参数存放在 /etc/logrotate.d 。

典型的应用有:apache, linuxconf, samba, cron 以及syslog。

这样,系统管理员只要管理一个 /etc/logrotate.conf 文件就可以了。

使用 prerotate 和 postrotate 选项

下面的例子是典型的脚本 /etc/logrotate.d/syslog,这个脚本只是对 /var/log/messages 有效。

1
2
3
4
5
6
7
8
9
10
/var/log/messages
{
	prerotate
		/usr/bin/chattr -a /var/log/messages
	endscript
	postrotate
		/usr/bin/kill -HUP syslogd
		/usr/bin/chattr +a /var/log/messages
	endscript
}

prerotate 命令指定转储以前的动作/usr/bin/chattr -a 去掉/var/log/messages文件的"只追加"属性 endscript 结束 prerotate 部分的脚本postrotate 指定转储后的动作

/usr/bin/killall -HUP syslogd

用来重新初始化系统日志守护程序 syslogd

/usr/bin/chattr +a /var/log/messages

重新为 /var/log/messages 文件指定"只追加"属性,这样防治程序员或用户覆盖此文件。

最后的 endscript 用于结束 postrotate 部分的脚本

高精度定时器示例

这样设置会更好???

1
nohz=off highres=off

http://blog.chinaunix.net/uid-361890-id-257337.html

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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/jiffies.h>

static struct hrtimer timer;
ktime_t kt;

static enum hrtimer_restart hrtimer_handler(struct hrtimer *timer)
{
	//kt = ktime_set(1, 10);
	printk(" ------ I am in hrtimer -----\n");
	hrtimer_forward(timer, timer->base->get_time(), kt);
	return HRTIMER_RESTART;
}

static int __init test_init(void)
{
	pr_info("timer resolution: %lu\n", TICK_NSEC);
	kt = ktime_set(1, 10); /* 1 sec, 10 nsec */
	hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	//hrtimer_set_expires(&timer, kt);
	hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
	timer.function = hrtimer_handler;

	printk("\n-------- test start ---------\n");
	return 0;
}

static void __exit test_exit(void)
{
	hrtimer_cancel(&timer);
	printk("-------- test over ----------\n");
	return;
}

MODULE_LICENSE("GPL");

module_init(test_init);
module_exit(test_exit);

编译:

1
2
3
4
5
6
7
8
9
10
name = test.o
obj-m := $(name)

KERNELDIR := /home/tangyt/tmp/linux-2.6-cloud.p1020/

default:
	make -C $(KERNELDIR) M=$(shell pwd) modules

clean:
	make -C $(KERNELDIR) M=$(shell pwd) clean

perf 火焰图分析程序性能

https://www.cnblogs.com/happyliu/p/6142929.html

使用火焰图展示结果

1、Flame Graph项目位于GitHub上:https://github.com/brendangregg/FlameGraph

2、可以用git将其clone下来:git clone https://github.com/brendangregg/FlameGraph.git

我们以perf为例,看一下flamegraph的使用方法:

1、第一步

1
sudo perf record -e cpu-clock -g -p 28591

Ctrl+c结束执行后,在当前目录下会生成采样数据perf.data.

2、第二步

用perf script工具对perf.data进行解析

1
perf script -i perf.data &> perf.unfold

3、第三步

将perf.unfold中的符号进行折叠:

1
./stackcollapse-perf.pl perf.unfold &> perf.folded

4、最后生成svg图:

1
./flamegraph.pl perf.folded > perf.svg