Linux的open file 与 file descriptor区别

1 open file

An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file.

2 file descriptor

A file descriptor is a data structure used by a program to get a handle on a file. The most commonly known are:
0 for standard in
1 for standard out
2 for standard error

前后两者关系
一个文件即使被打开,也可能没有文件描述符。
比如current working directories, memory mapped files and executable > text files。

3 区别

  • lsof 可以查出某个进程打开的文件数目(即 open file)。
root@testbox ~]# lsof | grep '4448'

oracle    4448  oracle  cwd       DIR        3,2      4096    1913941 /u01/app/oracle/product/10.2.0/db_1/dbs

oracle    4448  oracle  rtd       DIR        3,2      4096          2 /

oracle    4448  oracle  txt       REG        3,2  93300099    1915187 /u01/app/oracle/product/10.2.0/db_1/bin/oracle

oracle    4448  oracle  mem       REG        3,2     95148    2174926 /lib/libnsl-2.3.4.so

oracle    4448  oracle  mem       REG        3,2    106397    2174894 /lib/ld-2.3.4.so

oracle    4448  oracle  mem       REG        3,2   1454546    2501884 /lib/tls/libc-2.3.4.so

[root@testbox ~]# lsof | grep '4448' | wc -l

42                    //打开的文件数目
  • ls用于查询进程使用的文件描述符数目
[root@testbox ~]# ls -l /proc/4448/fd/

total 18

lr-x------  1 oracle oinstall 64 Mar  5 15:04 0 -> /dev/null

lr-x------  1 oracle oinstall 64 Mar  5 15:04 1 -> /dev/null

lrwx------  1 oracle oinstall 64 Mar  5 15:04 10 -> /u01/app/oracle/product/10.2.0/db_1/rdbms/audit/ora_4446.aud

lr-x------  1 oracle oinstall 64 Mar  5 15:04 11 -> /dev/zero

lr-x------  1 oracle oinstall 64 Mar  5 15:04 12 -> /dev/zero

:

[root@testbox ~]# ls -l /proc/4448/fd/ | wc -l

19                    //文件描述符数目 + 1 (注意还有一行 “total 18”)

4 查看文件描述符的设置

$cat /proc/sys/fs/file-max

65536

--该参数可以动态修改

5 计算当前被使用的文件描述符数目

$cat /proc/sys/fs/file-nr           //该文件只能读,不能修改

1380    180     65536

   |     |       |_ Max no. of file descriptors allowed on the system

   |     |          (与file-max一致)

   |     |__ Total free allocated file descriptors

   |

   |__  Total allocated file descriptors

To compute the number of file descriptors currently being used:

1380 - 180 = 1200

参考文献:
[1] http://blog.itpub.net/15480802/viewspace-734062/ 博主:myownstars

<think>好的,用户想要监控和排查Prometheus中当前打开的文件描述符的情况。首先,我需要回忆Prometheus的监控机制,知道它自身会暴露一些指标,其中可能包括文件描述符相关的。根据用户提供的引用[^1],RabbitMQ支持Prometheus和Grafana,这可能意味着Prometheus有相关的指标暴露方式。 接下来,我需要确定具体的指标名称。通常,Prometheus自身的指标可能包含process_open_fds,表示当前打开的文件描述符数量,还有process_max_fds,显示系统允许的最大数量。用户需要检查这些指标是否被正确暴露,并且能够在Prometheus的Web界面或者Grafana中查询到。 然后是配置告警规则,当打开的文件描述符接近最大值时触发警报。这需要编写PromQL表达式,比如(process_open_fds / process_max_fds) * 100 > 80,这样的规则。引用中提到的生产环境建议使用Prometheus和Grafana,所以这部分是可行的。 在排查步骤中,用户可能需要检查Prometheus的日志,是否有“too many open files”的错误,这通常意味着文件描述符耗尽。这时候需要调整系统的ulimit设置,增加最大文件描述符限制。同时,检查Prometheus的配置文件,确保数据存储路径正确,避免因存储问题导致文件未关闭。 另外,用户可能不知道如何查看Prometheus进程的当前文件描述符使用情况,可以使用lsof或/proc文件系统。这部分需要详细说明命令的使用方法,比如lsof -p <PID>或者查看/proc/<PID>/fd下的文件数量。 最后,生成相关问题时,要考虑用户可能的后续需求,比如如何优化配置、其他监控工具集成,或者如何调整系统参数等。需要确保问题覆盖监控、告警、故障排查和优化等方面,帮助用户全面了解处理文件描述符问题的方法。</think>### Prometheus当前打开文件描述符的监控排查方法 #### 1. **监控文件描述符的指标** Prometheus自身暴露了进程相关的指标,可通过以下关键指标监控文件描述符: - `process_open_fds`:当前打开的文件描述符数量 - `process_max_fds`:系统允许的最大文件描述符数量 在Prometheus Web界面或Grafana中执行以下查询: ```promql process_open_fds{job="prometheus"} process_max_fds{job="prometheus"} ``` #### 2. **配置告警规则** 在Prometheus的告警规则文件(`alert.rules.yml`)中添加: ```yaml - alert: HighFileDescriptors expr: (process_open_fds / process_max_fds) * 100 > 80 for: 5m labels: severity: warning annotations: summary: "Prometheus文件描述符使用率过高 (实例 {{ $labels.instance }})" description: "当前使用率 {{ $value }}%,接近系统限制" ``` #### 3. **排查步骤** - **检查操作系统限制**: ```bash # 查看Prometheus进程的当前限制 cat /proc/$(pgrep prometheus)/limits | grep 'Max open files' # 临时修改限制(重启失效) ulimit -n 65536 ``` 永久修改需在`/etc/security/limits.conf`中添加: ``` prometheus_user hard nofile 65536 prometheus_user soft nofile 65536 ``` - **检查文件句柄泄漏**: ```bash # 查看Prometheus进程打开的文件 lsof -p $(pgrep prometheus) | wc -l # 按类型统计 lsof -p $(pgrep prometheus) | awk '{print $5}' | sort | uniq -c ``` - **分析存储目录**: Prometheus可能因存储路径中的大量临时文件导致描述符耗尽,检查`--storage.tsdb.path`参数配置的目录: ```bash ls -l /var/lib/prometheus/wal | wc -l ``` #### 4. **优化建议** - 增加`storage.tsdb.retention.time`减少数据保留时间 - 使用`--storage.tsdb.wal-compression`启用WAL压缩 - 定期清理旧的快照文件(`snapshots`目录)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值