记录一次 docker 环境部署安装 easysnmp 包

一、项目背景

最近在做一个扫描数据中心设备(交换机。路由器、防火墙。。。)端口的功能。

需要用到 snmp 组件,Python 下有很多实现 snmp 的包,常用的有 pysnmpeasysnmp。由于pysnmp 的语法太恶心了,所以果断选择 easysnmpeasysnmp 需要依赖 C 语言的 net-snmp 库,所以速度还是很快的。

二、解决过程

在开发环境下(Mac 环境)直接使用 pip 安装即可。

pip install easysnmp

由于整个项目是使用微服务进行开发的,部署是使用Docker 容器的方式。DockerFile 内容如下:

FROM python:3.8

COPY . /app
COPY ./docker/sources.list /etc/apt/sources.list
RUN mkdir -p /app/logs

RUN apt-get update && apt-get install supervisor -y

COPY ./docker/supervisor_site.conf /etc/supervisor/conf.d
COPY ./docker/supervisord.conf /etc/supervisor/

WORKDIR /app

RUN pip3 install -r /app/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
EXPOSE 8000

CMD ["supervisord","-c","/etc/supervisor/supervisord.conf"]

果然在上测试线的时候出问题了。

easysnmp/interface.c:24:10: fatal error: net-snmp/net-snmp-config.h: No such file or directory
#include <net-snmp/net-snmp-config.h>
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'gcc' failed with exit status 1
----------------------------------------

从报错可以看出,是因为缺少 <net-snmp/net-snmp-config.h>,那就装一个好了。

因为 python:3.8 镜像的系统是 debian 的,所以使用 apt-get 安装。

Step 4/10 : RUN apt-get install net-snmp -y
 ---> Running in 949cd83c18e8
Reading package lists...
Building dependency tree...
Reading state information...
[91mE: Unable to locate package net-snmp
[0mThe command '/bin/sh -c apt-get install net-snmp -y' returned a non-zero code: 100

果然不是那么顺利,报错内容是没有这个包,这特么就奇了怪了,Centos 之前通过 yum 装的,可以直接安装 net-snmp 的,难道这里面还有猫腻?哦,我知道了,应该是源里没有这个软件包。那就使用阿里云的源替换一下,应该就没问题了吧。

deb http://mirrors.aliyun.com/debian stretch main contrib non-free
deb-src http://mirrors.aliyun.com/debian stretch main contrib non-free
deb http://mirrors.aliyun.com/debian stretch-updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian stretch-updates main contrib non-free
deb http://mirrors.aliyun.com/debian-security stretch/updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main contrib non-free

呃。。。

	[91mE: Unable to locate package net-snmp

还是一样的。

easysnmp GitHub 上找找看,看下有没有其他人遇到这个问题。

在这里插入图片描述


https://github.com/kamakazikamikaze/easysnmp/issues/110

靠!他叫我放弃!!!我怎么可能会放弃。。。继续找,欸,找到一个,还是作者回答的,看来靠谱。

在这里插入图片描述


https://github.com/kamakazikamikaze/easysnmp/issues/67

去看看 EasySNMP 文档。

在这里插入图片描述

哦哦,是这样啊,DebianCentOS 安装 net-snmp 还不一样。

On Debian / Ubuntu systems:

sudo apt-get install libsnmp-dev snmp-mibs-downloader

哈哈,终于成功。。。个屁~

Step 5/11 : RUN apt-get update && apt-get install libsnmp-dev snmp-mibs-downloader -y
 ---> Running in d02df4ee3b77
...
[91mE: Unable to correct problems,you have held broken packages.

包损坏了,还有依赖的问题,装依赖库 libpci-dev

The following packages have unmet dependencies:
 libpci-dev : Depends: libudev-dev (>= 196) but it is not going to be installed
[91mE: Unable to correct problems,you have held broken packages.

版本还不对。。。

等下,先缓缓,想一下问题到底出在哪里?

。。。

经过一段时间的思考,我觉得从头开始解决这个问题。

先从镜像开始,不使用封装好的镜像,要纯镜像。我选用 ubuntu:18.04 的镜像试试看。

	FROM ubuntu:18.04

然后替换软件包的源。

sources.list

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

经过多次尝试后证明刚刚的想法是可行的。

COPY ./docker/sources.list /etc/apt/sources.list
RUN apt-get update
RUN apt-get install gcc python3.8 python3-pip -y
RUN apt-get install libsnmp-dev snmp-mibs-downloader -y

最后安装 supervisor,项目中管理进程要用到。

RUN apt-get install supervisor -y

完了,又出问题了。基于 gevent 来启动 gunicorn 部署的 Flask 服务,在 Python2 版本下面的时候是正常启动的,但是在 Python3 版本下面启动测试的时候就报错了,报错信息如下所示:

pkg_resources.DistributionNotFound: The 'psutil>=5.7.0; sys_platform != "win32" or platform_python_implementation == "CPython" and extra == "recommended"' distribution was not found and is required by the application

根据报错输出定位到 gevent 使用的时候报错了,从错误信息里面其实可以猜出来一个大概就是:应该是某个模块缺失导致的报错,尝试安装这个模块 psutil,来观察错误是否解决。

RUN pip3 install psutil==5.7.2 -i https://pypi.douban.com/simple/

注意上面模块安装的版本信息,要求是 >=5.7.0,我安装的 5.7.2 满足要求,接下来重新执行发现问题解决了。

最后执行部署整个项目到测试线,发现一次通过,完美解决。

其实,真是的过程比写出来的还要坎坷。但我也一直在思考一个问题

原文地址:https://blog.csdn.net/yilovexing

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Jinja2:是Python的Web项目中被广泛应用的模板引擎,是由Python实现的模板语言,Jinja2 的作者也是 Flask 的作者。他的设计思想来源于Django的模板引擎,并扩展了其语法和一系列强大的功能,其是Flask内置的模板语言。
Fullcalendar日历使用,包括视图选择、事件插入、编辑事件、事件状态更改、事件添加和删除、事件拖动调整,自定义头部,加入el-popover显示图片、图片预览、添加附件链接等,支持手机显示。
监听QQ消息并不需要我们写代码,因为市面上已经有很多开源QQ机器人框架,在这里我们使用go-cqhttp官方文档:go-cqhttp如果您感兴趣的话,可以阅读一下官方文档,如果不想看,直接看我的文章即可。
【Flask框架】—— 视图和URL总结
python+web+flask轻量级框架的实战小项目。登录功能,后续功能可自行丰富。
有了这个就可以配置可信IP,关键是不需要企业认证,个人信息就可以做。
本专栏是对Flask官方文档中个人博客搭建进行的归纳总结,与官方文档结合事半功倍。 本人经验,学习一门语言或框架时,请首先阅读官方文档。学习完毕后,再看其他相关文章(如本系列文章),才是正确的学习道路。
本专栏是对Flask官方文档中个人博客搭建进行的归纳总结,与官方文档结合事半功倍。基础薄弱的同学请戳Flask官方文档教程 本人经验,学习一门语言或框架时,请首先阅读官方文档。学习完毕后,再看其他相关文章(如本系列文章),才是正确的学习道路。 如果python都完全不熟悉,一定不要着急学习框架,请首先学习python官方文档,一步一个脚印。要不然从入门到放弃是大概率事件。 Python 官方文档教程
快到年末了 相信大家都在忙着处理年末数据 刚好有一个是对超市的商品库存进行分析的学员案例 真的非常简单~
一个简易的问答系统就这样完成了,当然,这个项目还可以进一步完善,比如 将数据存入Elasticsearch,通过它先进行初步的检索,然后再通过这个系统,当然我们也可以用其他的架构实现。如果你对这系统还有其他的疑问,也可以再下面进行留言!!!
#模版继承和页面之间的调用@app.route(&quot;/bl&quot;)def bl(): return render_template(&quot;file_2.html&quot;)主ht
#form表达提交@app.route(&quot;/data&quot;,methods=[&#39;GET&#39;,&#39;POST&#39;]) #methods 让当前路由支持GET 和
#form表达提交@app.route(&quot;/data&quot;,methods=[&#39;GET&#39;,&#39;POST&#39;]) #methods 让当前路由支持GET 和
#session 使用app.secret_key = &quot;dsada12212132dsad1232113&quot;app.config[&#39;PERMANENT_SESSION_LI
#文件上传@app.route(&quot;/file&quot;,methods=[&#39;GET&#39;,&#39;POST&#39;])def file(): if request.meth
#跳转操作:redirect@app.route(&quot;/red&quot;)def red(): return redirect(&quot;/login&quot;)
#session 使用app.secret_key = &quot;dsada12212132dsad1232113&quot;app.config[&#39;PERMANENT_SESSION_LI
@app.route(&quot;/req&quot;,methods=[&#39;GET&#39;,&#39;POST&#39;])def req(): print(request.headers)
#模版继承和页面之间的调用@app.route(&quot;/bl&quot;)def bl(): return render_template(&quot;file_2.html&quot;)主ht
#文件操作:send_file,支持图片 视频 mp3 文本等@app.route(&quot;/img&quot;)def img(): return send_file(&quot;1.jpg&q