自开发了prometheus的vcenter exporter,vcenter大概有1300多台主机和虚拟机,每隔30秒都要发起一次ping的请求。在生产环境redhat7上部署之后出现too many open files报错,报错信息:
Jun 28 17:53:53 sz180591 observer_vcenter_exporter: time="2018-06-28T17:53:53+08:00" level=error msg="ping ip: 10.60.1.54 error:pipe2: too many open files" source="ping.go:57"Jun 28 17:53:53 sz180591 observer_vcenter_exporter: time="2018-06-28T17:53:53+08:00" level=error msg="ping ip: 10.60.0.187 error:fork/exec /usr/bin/ping: too many open files" source="ping.go:57"
在网上查找相关信息,均是通过"ulimit -n 2048"来修改"open files"的最大值。
但通过"ulimit -a"发现"open files"已经设置为了65536
sz180591:root@/root>ulimit -acore file size (blocks, -c) 0data seg size (kbytes, -d) unlimitedscheduling priority (-e) 0file size (blocks, -f) unlimitedpending signals (-i) 15210max locked memory (kbytes, -l) 64max memory size (kbytes, -m) unlimitedopen files (-n) 65536pipe size (512 bytes, -p) 8POSIX message queues (bytes, -q) 819200real-time priority (-r) 0stack size (kbytes, -s) unlimitedcpu time (seconds, -t) unlimitedmax user processes (-u) 65536virtual memory (kbytes, -v) unlimitedfile locks (-x) unlimited
后面通过google查询,发现有提到systemd会惹祸,大概的意思就是通过systemd启动的服务,不会使用ulimit中的配置,需要在systemd中或者service配置文件中定义,可以通过查看 /proc/<pid>/limits文件中的内容来确定。
刚好,该exporter就是通过systemd启动的,通过查看进程的limits,发现"Max open files"确实不是ulimit中的设置
sz180591:root@/var/log>cat /proc/5750/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 15210 15210 processes Max open files 1024 4096 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 15210 15210 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us
修改service定义,增加"LimitNOFILE=15210"
[Unit]Description=exporter vcenter serviceAfter=network.target[Service]LimitNOFILE=15210Type=simplePIDFile=/var/run/observer_vcenter_exporter.pidExecReload=/bin/kill -HUP $MAINPIDExecStart=/qhapp/monitor/exporter_vcenter/observer_vcenter_exporter --config-file=/qhapp/monitor/config/observer/vcenter.ymlSyslogIdentifier=observer_vcenter_exporterRestart=always[Install]WantedBy=multi-user.target
在查看进行的limits,可以看出"Max open files"已经修改。
sz180591:root@/var/log>cat /proc/15986/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 15210 15210 processes Max open files 15210 15210 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 15210 15210 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us
经过一段时间的观察,暂未出现"too many open files"的异常了。