1、Prometheus 监控系统(上)

news/2025/2/15 15:14:38/

Prometheus 监控系统(上)

      • 认识一下 Prometheus
        • Prometheus 的特点
        • Prometheus 的生态组件
        • Prometheus 的工作模式
        • Prometheus 的工作流程
        • Prometheus 的局限性:
      • 部署 Prometheus
        • Prometheust Server 端安装和相关配置
        • 部署 Exporters
          • 部署 Node Exporter 监控系统级指标
        • 监控 MySQL 配置示例
          • 安装mysql
          • 部署 mysqld_exporter
          • 在 Prometheus 服务器上操作
        • 监控 Nginx 配置示例
          • 源代码编译安装 nginx
          • 部署 nginx-exporter
          • 在 Prometheus 服务器上操作

认识一下 Prometheus

Prometheus 是一个开源的服务监控系统和时序数据库,其提供了通用的数据模型和快捷数据采集、存储和查询接口。
它的核心组件 Prometheus server 会定期从静态配置的监控目标或者基于服务发现自动配置的目标中进行拉取数据,新拉取到的数据会持久化到存储设备当中。

每个被监控的主机都可以通过专用的 exporter 程序提供输出监控指标数据的接口,它会在目标处收集监控数据,并暴露出一个 HTTP 接口供 Prometheus server 查询, Prometheus 通过基于 HTTP 的 pull 的方式来周期性的采集数据,默认时间是 15s 抓取一次。
抓取到的指标数据会被以时间序列的形式保存在内存中,并且定时刷到磁盘上,默认是两个小时回刷一次。并且为了防止 Prometheus 发生崩溃或重启时能够恢复数据, Prometheus 也提供了类似 MySQL 中 binlog 一样的 wal 预写日志,当 Prometheus 崩溃重启时,会读这个预写日志来恢复数据。

如果存在告警规则,则抓取到数据之后会根据规则进行计算,满足告警条件则会生成告警,并发送到 Alertmanager 完成告警的汇总和分发。

对于短时间执行的脚本任务或者不好直接 pull 指标数据的服务,Prometheus 提供了 Pushgateway 给这些任务将服务指标数据主动 push 到 Pushgateway 并临时存储,然后等待 Prometheus server 完成数据的采集。

任何被监控的目标都需要事先纳入到监控系统中才能进行时序数据采集、存储、告警和展示,监控目标可以通过配置信息以静态形式指定,也可以让 Prometheus 通过服务发现的机制进行动态管理。
Prometheus 能够直接把K8S的 API Server 作为服务发现系统使用,进而动态发现和监控K8S集群中的所有可被监控的对象。

Prometheus 官网地址:https://prometheus.io
Prometheus github 地址:https://github.com/prometheus

Prometheus 的特点

1、多维数据模型:由度量名称和键值对标识的时间序列数据
时间序列数据:按照时间顺序记录系统、设备状态变化的数据,每个数据称为一个样本;服务器指标数据、应用程序性能监控数据、网络数据等都是时序数据
2、内置时间序列(Time Series)数据库:Prometheus ;外置的远端存储通常会用:InfluxDB、OpenTSDB 等
3、promQL 一种灵活的查询语言,可以利用多维数据完成复杂查询
4、基于 HTTP 的 pull(拉取)方式采集时间序列数据
5、同时支持 PushGateway 组件收集数据
6、通过静态配置或服务发现发现目标
7、支持作为数据源接入 Grafana

Prometheus 的生态组件

Prometheus Server 负责时序型指标数据的采集及存储,但数据的分析、聚合及直观展示以及告警等功能并非由 Prometheus Server 所负责。
Prometheus 生态圈中包含了多个组件,其中部分组件可选:
(1)Prometheus server:服务核心组件,采用 pull 方式采集监控数据,通过 http 协议传输;存储时间序列数据;基于“告警规则”生成告警通知。
(2)Client Library: 客户端库,目的在于为那些期望原生提供 Instrumentation 功能的应用程序提供便捷的开发途径,用于基于应用程序内建的测量系统。
(3)Exporters:指标暴露器,负责收集不支持内建 Instrumentation 的应用程序或服务的性能指标数据,并通过 HTTP 接口供 Prometheus Server 获取。
换句话说,Exporter 负责从目标应用程序上采集和聚合原始格式的数据,并转换或聚合为 Prometheus 格式的指标向外暴露。
(4)Service Discovery:服务发现,用于动态发现待监控的 Target,Prometheus 支持多种服务发现机制:文件、DNS、Consul、Kubernetes 等等。 服务发现可通过第三方提供的接口,Prometheus 查询到需要监控的 Target 列表,然后轮询这些 Target 获取监控数据。该组件目前由 Prometheus Server 内建支持
(5)Alertmanager:是一个独立的告警模块,从 Prometheus server 端接收到 “告警通知” 后,会进行分组、去重,并路由到相应的接收方,发出报警, 常见的接收方式有:电子邮件、钉钉、企业微信等。
Prometheus Server 仅负责生成告警指示,具体的告警行为由另一个独立的应用程序 AlertManager 负责;告警指示由 Prometheus Server 基于用户提供的告警规则周期性计算生成,Alertmanager 接收到 Prometheus Server 发来的告警指示后,基于用户定义的告警路由向告警接收人发送告警信息。
(6)Pushgateway:类似一个中转站,Prometheus 的 server 端只会使用 pull 方式拉取数据,但是某些节点因为某些原因只能使用 push 方式推送数据, 那么它就是用来接收 push 而来的数据并暴露给 Prometheus 的 server 拉取的中转站。
可以理解成目标主机可以上报短期任务的数据到 Pushgateway,然后 Prometheus server 统一从 Pushgateway 拉取数据。
(7)Grafana:是一个跨平台的开源的度量分析和可视化工具,可以将采集的数据可视化的展示,并及时通知给告警接收方。其官方库中具有丰富的仪表盘插件。

Prometheus 的工作模式

1、Prometheus Server 基于服务发现(Service Discovery)机制或静态配置获取要监视的目标(Target),并通过每个目标上的指标 exporter 来采集(Scrape)指标数据;
2、Prometheus Server 内置了一个基于文件的时间序列存储来持久存储指标数据,用户可使用 PromQL 接口来检索数据,也能够按需将告警需求发往 Alertmanager 完成告警内容发送;
3、一些短期运行的作业的生命周期过短,难以有效地将必要的指标数据供给到 Server 端,它们一般会采用推送(Push)方式输出指标数据, Prometheus 借助于 Pushgateway 接收这些推送的数据,进而由 Server 端进行抓取

Prometheus 的工作流程

(1)Prometheus 以 Prometheus Server 为核心,用于收集和存储时间序列数据。Prometheus Server 从监控目标中通过 pull 方式拉取指标数据,或通过 pushgateway 把采集的数据拉取到 Prometheus server 中。
(2)Prometheus server 把采集到的监控指标数据通过 TSDB 存储到本地 HDD/SSD 中。
(3)Prometheus 采集的监控指标数据按时间序列存储,通过配置报警规则,把触发的告警通知发送到 Alertmanager。
(4)Alertmanager 通过配置报警接收方,发送报警到邮件、钉钉或者企业微信等。
(5)Prometheus 自带的 Web UI 界面提供 PromQL 查询语言,可查询监控数据。
(6)Grafana 可接入 Prometheus 数据源,把监控数据以图形化形式展示出。

Prometheus 的局限性:

1、Prometheus 是一款指标监控系统,不适合存储事件及日志等;它更多地展示的是趋势性的监控,而非精准数据;
2、Prometheus 认为只有最近的监控数据才有查询的需要,其本地存储的设计初衷只是保存短期(例如一个月)数据,因而不支持针对大量的历史数据进行存储;若需要存储长期的历史数据,建议基于远端存储机制将数据保存于 InfluxDB 或 OpenTSDB 等系统中;
3、Prometheus 的集群机制成熟度不高,可基于 Thanos 或 Cortex 实现 Prometheus 集群的高可用及联邦集群。

部署 Prometheus

Prometheus:192.168.110.50

Prometheust Server 端安装和相关配置

环境准备

#关闭防火墙和核心防护
systemctl stop firewalld
setenforce 0#上传 prometheus-2.45.0.linux-amd64.tar.gz 到 /opt 目录中,并解压
cd /opt/
tar xf prometheus-2.45.0.linux-amd64.tar.gz
mv prometheus-2.45.0.linux-amd64 /usr/local/prometheus

Prometheust Server 端安装

#查看配置文件
cat /usr/local/prometheus/prometheus.yml | grep -v "^#"
global:					#用于prometheus的全局配置,比如采集间隔,抓取超时时间等scrape_interval: 15s			#采集目标主机监控数据的时间间隔,默认为1mevaluation_interval: 15s 		#触发告警生成alert的时间间隔,默认是1m# scrape_timeout is set to the global default (10s).scrape_timeout: 10s			#数据采集超时时间,默认10salerting:				#用于alertmanager实例的配置,支持静态配置和动态服务发现的机制alertmanagers:- static_configs:- targets:# - alertmanager:9093rule_files:				#用于加载告警规则相关的文件路径的配置,可以使用文件名通配机制# - "first_rules.yml"# - "second_rules.yml"scrape_configs:			#用于采集时序数据源的配置# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.- job_name: "prometheus"		#每个被监控实例的集合用job_name命名,支持静态配置(static_configs)和动态服务发现的机制(*_sd_configs)# metrics_path defaults to '/metrics'metrics_path: '/metrics'    #指标数据采集路径,默认为 /metrics# scheme defaults to 'http'.static_configs:				#静态目标配置,固定从某个target拉取数据- targets: ["localhost:9090"]

配置系统启动文件,启动 Prometheust

cat > /usr/lib/systemd/system/prometheus.service <<'EOF'
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data/ \
--storage.tsdb.retention.time=15d \
--web.enable-lifecycleExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure[Install]
WantedBy=multi-user.target
EOF

启动

systemctl start prometheus
systemctl enable prometheus

查看端口是否开启

netstat -natp | grep :9090[root@etcd01 opt]# netstat -natp | grep :9090
tcp6       0      0 :::9090                 :::*                    LISTEN      117476/prometheus

浏览器访问:
http://192.168.110.50:9090 ,访问到 Prometheus 的 Web UI 界面
Prometheus 的 Web UI 界面

点击页面的 Status -> Targets,如看到 Target 状态都为 UP,说明 Prometheus 能正常采集到数据
Prometheus 能正常采集到数据

http://192.168.110.50:9090/metrics ,可以看到 Prometheus 采集到自己的指标数据,其中 Help 字段用于解释当前指标的含义,Type 字段用于说明数据的类型
以 go_ 为前缀的指标是关于 Go 运行时相关的指标,比如垃圾回收时间、goroutine 数量等,这些都是 Go 客户端库特有的

Prometheus 采集到自己的指标数据

部署 Exporters

node 节点 IP
node01:192.168.110.20
node01:192.168.110.30

部署 Node Exporter 监控系统级指标

环境准备

#关闭防火墙和核心防护
systemctl stop firewalld
setenforce 0#上传 node_exporter-1.3.1.linux-amd64.tar.gz 到 /opt 目录中,并解压
cd /opt/
tar xf node_exporter-1.3.1.linux-amd64.tar.gz
mv node_exporter-1.3.1.linux-amd64/node_exporter /usr/local/bin

配置启动文件

cat > /usr/lib/systemd/system/node_exporter.service <<'EOF'
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter \
--collector.ntp \
--collector.mountstats \
--collector.systemd \
--collector.tcpstatExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure[Install]
WantedBy=multi-user.target
EOF

启动

systemctl start node_exporter
systemctl enable node_exporter

查看端口

netstat -natp | grep :9100[root@node01 ~]# netstat -natp | grep :9100
tcp6       0      0 :::9100                 :::*                    LISTEN      11194/node_exporter

浏览器访问:http://192.168.110.20:9100/metrics ,可以看到 Node Exporter 采集到的指标数据
在这里插入图片描述
常用的各指标:
●node_cpu_seconds_total
●node_memory_MemTotal_bytes
●node_filesystem_size_bytes{mount_point=PATH}
●node_system_unit_state{name=}
●node_vmstat_pswpin:系统每秒从磁盘读到内存的字节数
●node_vmstat_pswpout:系统每秒钟从内存写到磁盘的字节数
更多指标介绍:https://github.com/prometheus/node_exporter

修改 prometheus 配置文件,加入到 prometheus 监控中

vim /usr/local/prometheus/prometheus.yml
#在尾部增加如下内容- job_name: nodesmetrics_path: "/metrics"static_configs:- targets:- 192.168.110.20:9100- 192.168.110.30:9100labels:service: kubernetes

重新载入配置
curl -X POST http://192.168.110.50:9090/-/reload 或 systemctl reload prometheus
浏览器查看 Prometheus 页面的 Status -> Targets
在这里插入图片描述

监控 MySQL 配置示例

mysql:192.168.110.60

安装mysql
#上传软件包到/opt,并解压
tar -xf mysql-8.0.33-el7-x86_64.tar.gzmv mysql-8.0.33-el7-x86_64 mysql
mv mysql /usr/local/#创建程序用户管理
useradd -s /sbin/nologin mysql#修改mysql目录和配置文件的权限
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf#修改配置文件(linux 可能自带数据库mariadb,需删除 yum remove -y mariadb* )
vim /etc/my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION#设置环境变量,申明/宣告mysql命令便于系统识别
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile#初始化数据库
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data#设置系统识别
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
systemctl daemon-reload
systemctl restart mysqld
#初始化数据库密码
mysqladmin -u root -p password "123456"
#进入数据库
mysql -u root -p123456
#创建用户并设置密码
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
#赋予远程连接的权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
#刷新生效
flush privileges;
#修改加密方式,可以进行远程连接
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
部署 mysqld_exporter

环境准备

#关闭防火墙和核心防护
systemctl stop firewalld
setenforce 0#上传 mysqld_exporter-0.14.0.linux-amd64.tar.gz 到 /opt 目录中,并解压
cd /opt/
tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz
mv mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter /usr/local/bin/

配置启动文件

cat > /usr/lib/systemd/system/mysqld_exporter.service <<'EOF'
[Unit]
Description=mysqld_exporter
Documentation=https://prometheus.io/
After=network.target[Service]
Type=simple
ExecStart=/usr/local/bin/mysqld_exporter --config.my-cnf=/etc/my.cnfExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure[Install]
WantedBy=multi-user.target
EOF

修改 MySQL 配置文件

vim /etc/my.cnf
[client]
......
host=localhost
user=exporter
password=abc123

授权 exporter 用户

mysql -uroot -pabc123CREATE USER 'exporter'@'%' IDENTIFIED BY 'abc123';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';

重启服务

systemctl restart mysqld
systemctl start mysqld_exporter
systemctl enable mysqld_exporter

查看端口

netstat -natp | grep :9104
在 Prometheus 服务器上操作

修改 prometheus 配置文件,加入到 prometheus 监控中

vim /usr/local/prometheus/prometheus.yml
#在尾部增加如下内容- job_name: mysqldmetrics_path: "/metrics"static_configs:- targets:- 192.168.110.60:9104labels:service: mysqld

重新载入配置
curl -X POST http://192.168.110.50:9090/-/reload 或 systemctl reload prometheus
浏览器查看 Prometheus 页面的 Status -> Targets
在这里插入图片描述

监控 Nginx 配置示例

nginx:192.168.110.20

源代码编译安装 nginx

下载依赖包,上传软件包到 /opt 并解压

yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ makecd /opt
tar xf nginx-1.24.0.tar.gz

创建管理用户

useradd -M -s /sbin/nologin nginx

上传 nginx-module-vts-0.1.18.tar.gz 到 /opt 目录中,并解压

cd /opt
tar xf nginx-module-vts-0.1.18.tar.gz
mv nginx-module-vts-0.1.18 /usr/local/nginx-module-vts

配置安装路径

cd nginx-1.24.0/
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--add-module=/usr/local/nginx-module-vts

编译安装

make & make install

修改 nginx 配置文件

vim /usr/local/nginx/conf/nginx.confhttp {vhost_traffic_status_zone;					#添加vhost_traffic_status_filter_by_host on;		#添加,开启此功能,在 Nginx 配置有多个 server_name 的情况下,会根据不同的 server_name 进行流量的统计,否则默认会把流量全部计算到第一个 server_name 上......server {......}server {vhost_traffic_status off;		#在不想统计流量的 server 区域,可禁用 vhost_traffic_statuslisten 8080;allow 127.0.0.1;allow 192.168.110.50;			#设置为 prometheus 的 ip 地址location /nginx-status {stub_status on;access_log off;}location /status {vhost_traffic_status_display;vhost_traffic_status_display_format html;}}
}
#假如 nginx 没有规范配置 server_name 或者无需进行监控的 server 上,那么建议在此 vhost 上禁用统计监控功能。否则会出现 127.0.0.1、hostname 等的域名监控信息。#检查配置文件
nginx -t

软连接,命令识别

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

配置启动文件

cat > /lib/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx
After=network.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target
EOF

启动 nginx

systemctl start nginx
systemctl enable nginx

浏览器访问:http://192.168.110.20:8080/status ,可以看到 Nginx Vhost Traffic Status 的页面信息
在这里插入图片描述

部署 nginx-exporter

环境准备

#关闭防火墙和核心防护
systemctl stop firewalld
setenforce 0#上传 nginx-vts-exporter-0.10.3.linux-amd64.tar.gz 到 /opt 并解压
cd /opt/
tar -zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
mv nginx-vts-exporter-0.10.3.linux-amd64/nginx-vts-exporter /usr/local/bin/

配置启动文件

cat > /usr/lib/systemd/system/nginx-exporter.service <<'EOF'
[Unit]
Description=nginx-exporter
Documentation=https://prometheus.io/
After=network.target[Service]
Type=simple
ExecStart=/usr/local/bin/nginx-vts-exporter -nginx.scrape_uri=http://localhost:8080/status/format/jsonExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure[Install]
WantedBy=multi-user.target
EOF

启动

systemctl start nginx-exporter
systemctl enable nginx-exporter

查看端口

netstat -natp | grep :9913[root@node01 ~]# netstat -natp | grep :9913
tcp6       0      0 :::9913                 :::*                    LISTEN      10001/nginx-vts-exp
在 Prometheus 服务器上操作

修改 prometheus 配置文件,加入到 prometheus 监控中

vim /usr/local/prometheus/prometheus.yml
#在尾部增加如下内容- job_name: nginxmetrics_path: "/metrics"static_configs:- targets:- 192.168.110.20:9913labels:service: nginx

重新载入配置
curl -X POST http://192.168.110.50:9090/-/reload 或 systemctl reload prometheus
浏览器查看 Prometheus 页面的 Status -> Targets
在这里插入图片描述


http://www.ppmy.cn/news/1572268.html

相关文章

基于SpringBoot的电影院售票管理系统

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、Vue项目源码、SSM项目源码、微信小程序源码 精品专栏&#xff1a;…

Ubuntu 24.04 上安装 Nginx

在 Ubuntu 24.04 上安装 Nginx&#xff0c;可以按照以下步骤进行&#xff1a; 更新系统包 首先&#xff0c;确保你的包管理器是最新的。打开终端并执行&#xff1a; sudo apt update sudo apt upgrade安装 Nginx 安装 Nginx 只需要运行以下命令&#xff1a; sudo apt install n…

基于Flask的影视剧热度数据可视化分析系统的设计与实现

【FLask】基于Flask的影视剧热度数据可视化分析系统的设计与实现&#xff08;完整系统源码开发笔记详细部署教程&#xff09;✅ 目录 一、项目简介二、项目界面展示三、项目视频展示 一、项目简介 随着互联网技术的飞速发展&#xff0c;影视剧行业的数据量呈爆炸性增长&#x…

好好说话:深度学习扫盲

大创项目是和目标检测算法YOLO相关的&#xff0c;浅浅了解了一些有关深度学习的知识。在这里根据本人的理解做一些梳理。 深度学习是什么&#xff1f; 之前经常听到AI&#xff0c;机器学习&#xff0c;深度学习这三个概念&#xff0c;但是对于三者的区别一直很模糊。 AI&…

聊聊 IP 地址和端口号的区别

在计算机网络中&#xff0c;两个基本概念对于理解设备如何通过网络进行通信至关重要。IP 地址和端口号是 TCP/IP 的典型特征&#xff0c;其定义如下&#xff1a;IP 地址是分配给连接到网络的每台机器的唯一地址&#xff0c;用于定位机器并与其通信。相反&#xff0c;端口号用于…

AnythingLLM打造私有知识库

一、 OllamaDeepSeek安装 https://blog.csdn.net/Stestack/article/details/145405151?spm1001.2014.3001.5502二、 nomic-embed-text nomic-embed-text主要用于文本嵌入和句子相似度任务&#xff0c;能够将这些任务应用于分类、聚类、检索等多种自然语言处理场景。该模型在…

C++ 11原子变量

原子变量 原子变量的解释原子变量类 atomic禁止拷贝赋值和读取操作通过函数特化类型 使用初始化赋值读取操作对象&#xff08;指针访问&#xff09; 使用原子变量解决乱序问题内存循序约束 原子变量的解释 原子作为长期最小的单位&#xff0c;在这里被定义为不可分割的最小操作…

day51 第十一章:图论part02

99. 岛屿数量 深搜 每一块的上下左右都遍历过了之后&#xff0c;这块陆地就遍历完了。是深搜&#xff0c;不是广搜 深搜&#xff1a;递归 def dfs(): if .....: 终止条件 dfs(子节点) directions [[0,1],[1,0],[0,-1],[-1,0]]def dfs(grid, visited, x, y):if grid[x][y]…