侧边栏壁纸
博主头像
v林羽博主等级

行动起来,活在当下

  • 累计撰写 171 篇文章
  • 累计创建 34 个标签
  • 累计收到 12 条评论

目 录CONTENT

文章目录

【Docker项目】之--Glances系统资源监控程序支持API调用

v林羽
2022-12-04 / 0 评论 / 0 点赞 / 539 阅读 / 9533 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-11-01,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

【Docker项目】之–Glances系统资源监控程序支持API调用

#手册 #教程 #工具 #Docker #Docker-compose #Linux #Ubuntu #系统 #安全防护

Glances是一个由python开发的跨平台系统资源监控程序,支持web查看、系统数据API调用等功能。

官网地址:https://nicolargo.github.io/glances

Github地址:https://github.com/nicolargo/glances

文档地址:https://glances.readthedocs.io/en/latest

Docker Hub:https://hub.docker.com/r/nicolargo/glances

1. 安装准备

系统:ubuntu 20.4
工具:docker和docker-compose

2. 搭建

Docer控制台运行效果。

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock:ro --pid host --network host -it nicolargo/glances:latest-full

下面通过docker-compose搭建,实现web和API调用。

2.1. 创建数据存放位置

mkdir -p /data/docker_data/glances   ## 创建文件夹

cd /data/docker_data/glances   ## 进入文件夹

vim docker-compose.yml   ## 创建docker-compose

2.2. 编辑docker-compose.yml

version: '3'

services:
  glances:
    image: nicolargo/glances:latest-full
    container_name: glances
    restart: always
    ports:
      - 61208-61209:61208-61209
    pid: host
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - "GLANCES_OPT=-w"
    networks:
      - mynet

networks:                           # 连接外部网络,方便内部代理
  mynet:
    external: true

2.3. 新建docker网络

已经创建的略过。

# 创建一个公用的docker网络
docker network create mynet

# 查看docker网络情况
docker network ls

# 显示如下信息
NETWORK ID     NAME      DRIVER    SCOPE
0134ad30defd   bridge    bridge    local
29474e75ede0   host      host      local
33bc01a2e0d0   mynet     bridge    local    # 刚刚新建的网络

2.4. 拉去镜像开始安装

docker-compose up -d

3. 使用及展示

打开浏览器访问:http://ip:61208

4. 设置用户密码访问

删除已创建的glances容器。

root@ubuntu:/data/docker_data/glances# docker-compose down

4.1. 创建密码存放位置

## 创建密码文件存放位置
root@ubuntu:/data/docker_data/glances# mkdir -p /data/docker_data/glances/secrets
root@ubuntu:/data/docker_data/glances# vim docker-compose.yml

4.2. 编辑docker-compose.yml

version: '3'

services:
  glances:
    image: nicolargo/glances:latest-full
    container_name: glances
    restart: always
    ports:
      - 61208-61209:61208-61209
    pid: host
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - "GLANCES_OPT=-w --password"
    secrets:
      - source: glances_password
        target: /root/.config/glances/glances.pwd
    networks:
      - mynet

secrets:
    glances_password:
      file: ./secrets/glances_password

networks:                           # 连接外部网络,方便内部代理
  mynet:
    external: true

4.3. 获取密码文件

## 运行glances项目
root@ubuntu:/data/docker_data/glances# docker run -d -e GLANCES_OPT="-w" -v /var/run/docker.sock:/var/run/docker.sock:ro  --name="glances" --pid host -it nicolargo/glances:latest-full
1b1e167607920b39da3f926cb9bb3c4ca5106a85bd1faa2d8077a6dec8863908

## 进入容器内部
root@ubuntu:/data/docker_data/glances# docker exec -it glances sh

## 设置密码,默认用户名为glances,可以通过--username修改(后续设置复杂不推荐)
/glances # glances -s --password

## 输入两次密码,并保存
Define the Glances server password (glances username): 
Password (confirm): 
Do you want to save the password? [Yes/No]: yes
Glances XML-RPC server is running on 0.0.0.0:61209
Announce the Glances server on the LAN (using 172.17.0.2 IP address)

## ctrl+c结束任务
^C^C^C^C^C
Exception ignored in: <module 'threading' from '/usr/lib/python3.10/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1567, in _shutdown
    lock.acquire()
  File "/usr/lib/python3.10/site-packages/glances/__init__.py", line 65, in __signal_handler
    end()
  File "/usr/lib/python3.10/site-packages/glances/__init__.py", line 71, in end
    mode.end()
  File "/usr/lib/python3.10/site-packages/glances/server.py", line 234, in end
    self.autodiscover_client.close()
  File "/usr/lib/python3.10/site-packages/glances/autodiscover.py", line 238, in close
    self.zeroconf.unregister_service(self.info)
  File "/usr/lib/python3.10/site-packages/zeroconf/_core.py", line 699, in unregister_service
    run_coro_with_timeout(
  File "/usr/lib/python3.10/site-packages/zeroconf/_utils/asyncio.py", line 86, in run_coro_with_timeout
    raise EventLoopBlocked from ex
zeroconf._exceptions.EventLoopBlocked: 

## 退出容器
/glances # exit
root@ubuntu:/data/docker_data/glances# 

## 复制生成的密码文件到宿主机
root@ubuntu:/data/docker_data/glances# docker cp glances:/root/.config/glances/glances.pwd ./secrets/glances_password

## 删除容器
root@ubuntu:/data/docker_data/glances# docker rm -f glances
glances

4.4. 拉去镜像开始安装

root@ubuntu:/data/docker_data/glances# docker-compose up -d
[+] Running 1/1
 ⠿ Container glances  Started                                                                                                                                   0.4s
root@ubuntu:/data/docker_data/glances# 

4.5. 登录效果

5. API调用

详细的调用方式查看文档:API (Restfull/JSON) documentation — Glances 3.3.0.1 documentation

root@ubuntu:~# curl http://glances:testglances@192.168.100.111:61208/api/3/diskio
[{"time_since_update": 2.9525327682495117, "disk_name": "sr0", "read_count": 0, "write_count": 0, "read_bytes": 0, "write_bytes": 0, "key": "disk_name"}, {"time_since_update": 2.9525327682495117, "disk_name": "sda", "read_count": 0, "write_count": 0, "read_bytes": 0, "write_bytes": 0, "key": "disk_name"}, {"time_since_update": 2.9525327682495117, "disk_name": "sda1", "read_count": 0, "write_count": 0, "read_bytes": 0, "write_bytes": 0, "key": "disk_name"}, {"time_since_update": 2.9525327682495117, "disk_name": "sda2", "read_count": 0, "write_count": 0, "read_bytes": 0, "write_bytes": 0, "key": "disk_name"}, {"time_since_update": 2.9525327682495117, "disk_name": "sda3", "read_count": 0, "write_count": 0, "read_bytes": 0, "write_bytes": 0, "key": "disk_name"}, {"time_since_update": 2.9525327682495117, "disk_name": "dm-0", "read_count": 0, "write_count": 0, "read_bytes": 0, "write_bytes": 0, "key": "disk_name"}]
root@ubuntu:~# 

6. 关闭web访问

如果仅需要API调用,通过修改docker-compose.yml关闭web访问。

    environment:
      - "GLANCES_OPT=-w --password --disable-webui"
0

评论区