为了测试整个系统的稳定性,写了一个脚本,模仿整个登录过程,中间出错就报错 了解到一个好工具 selenium如何在centos上安装使用selenium-1 如何在centos上安装使用selenium-2
为了更为精确的监控网页,考虑使用selenium模拟打开每个页面去检查其中网络请求是否正常获得,最终的目标是获取类似chrome开发者模式中Network中的效果,但是经调查发现,selenium中没有对这部分进行实现(或者与其最初目标无关,不考虑实现),找到的可行的方案是使用 selenium + browsermob-proxy,browsermob-proxy 官方文档 browsermob-proxy 下载
示例代码
记录下中间遇到的问题:
端口冲突,报错
1 2 3 4 5 6 7 8 9 10
Traceback (most recent call last): File "use_for_temporary.py", line 160, in <module> selenium_example() File "use_for_temporary.py", line 81, in selenium_example proxy=server.create_proxy() File "/root/anaconda3/lib/python3.7/site-packages/browsermobproxy/server.py", line 40, in create_proxy client = Client(self.url[7:], params) File "/root/anaconda3/lib/python3.7/site-packages/browsermobproxy/client.py", line 38, in __init__ self.port = jcontent['port'] KeyError: 'port'
经过查看源码,大约猜测是网络端口的问题(后来发现在产生的server.log 中把问题说的很清楚 ( ╯□╰ )),查看相关文档确定 browsermob-proxy 启动的端口默认是8080 相关文档 ,而服务器中已经使用的端口包括这个,考虑更改其启动的配置参数,按照上述文档的方法下修改,无效报错,经过查看与browsermob-proxy 版本对应 的官方文档Server 初始化 以及相应解析源码,重新修改配置 为
1
server=Server("/root/download/browsermob-proxy-2.0-beta-6/bin/browsermob-proxy",{"host":"localhost","port":9000})
成功
文件引用问题
1 2 3 4 5 6 7 8 9 10
Traceback (most recent call last): File "use_for_temporary.py", line 159, in <module> selenium_example() File "use_for_temporary.py", line 79, in selenium_example server.start() File "/root/anaconda3/lib/python3.7/site-packages/browsermobproxy/server.py", line 122, in start raise ProxyServerError(message) browsermobproxy.exceptions.ProxyServerError: The Browsermob-Proxy server process failed to start. Check <_io.TextIOWrapper name='/root/py_auto/Gateway_auto/server.log' mode='w' encoding='UTF-8'>for a helpful error message. # server.log 错误: 找不到或无法加载主类 org.browsermob.proxy.Main
原因:我将browsermob-proxy解压后,将其中bin目录下browsermobproxy专门提取出来,放在一个专门目录下(以为是可执行文件),引用之,后来发现它和windows下使用的browsermobproxy.bat 一样都是个脚本文件,需要在项目的固定位置 解决:将引用目录指向目录文件下的browsermobproxy
工具版本适配问题
问题:使用browsermob-proxy +selenium 时候,出现网络请求异常,在查看browsermob-proxy 产生的har记录文件时发现某些网路请求异常,返回-999(no response)或者0,不是正常的返回,但是单独用requests 模块去做请求时,又没有问题,于是考虑是因为webdriver结束过早,考虑过强制sleep,以及 driver.implict_wait(),但是都起不到作用,或者不够明显。 后来思考了下,我想要做的事情是判断页面是否加载正常,静态数据大多没有太多问题,主要是动态数据,即我所关注或者担心的大部属分请求都是ajax异步请求,前面那些方法可能没有太多作用,于是查找是否有有相关手段使selenium 等待所有ajax请求结束,其中提到了selenium的三种等待方式:强制等待(sleep等方法),隐式等待(driver.implict_wait(),全局效果),显式等待(在一定时间内等待某一元素的出现) 最终采取的是第三种方法,
WebDriverWait(driver, 30).until(lambda s: s.execute_script(“return jQuery.active == 0”))
参考博客
但是在centos服务器上加上这行代码会出现 jQuery/$(如果使用 $.active) no defined 最开始考虑类似与之前的方法,是因为这段脚本在jQuery加载之前执行引起的错误,后来尝试过强制等待后添加这行代码,无论等待多长时间都没有效果 ,开始怀疑是因为服务器环境的特殊性,于是在window笔记本,ubuntu台式机上尝试都没有异常,然后开始怀疑是环境配置的问题,其中包含的环境配置主要有selenium ,chromdriver,google-chrome,browsermob-proxy 由于selenium都是用pip安装的最新的,不考虑 然后发现centos服务器上的google-chrome是最新版76.0.3809.12 ,而windows上是75.0.3770.8,ubuntu上chromium是 v74.0.3729.6然后依据windowsde 配置调整了chromdriver,google-chrome的配置,但是仍然没有效果,最后发现centos 上的browsermob-proxy 是2.0-beta版本,而其他两个是2.14版本,最后调整之后,发现运行没有问题(暂时弄不明白为什么) 记录下正确的版本配置: selenium: 3.141.0 chromdriver:75.0.3770.90 google-chrome: 75.0.3770.142 browsermob-proxy:2.1.4
日志增长的问题
考虑到网关日志增长过大的可能性,考虑到使用脚本定期定量切割日志,重命名,创建新文件,后来发现linux系统中有现成工具 logrotate 可以对日志进行切割,里面的配置完全可以满足个人需求 可以使用 man logrotate 查看配置信息 或者 logrotate Linxu 官网档案
个人配置
位置:/etc/logrotate.d/kong
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/usr/local/kong/logs/access.log { missingok rotate 200 size 10M compress delaycompress notifempty create 644 root root dateext sharedscripts postrotate if [ -f /var/run/nginx.pid ]; then kill -USR1 `cat /usr/local/kong/pids/nginx.pid` fi endscript }
原本为 cat /var/run/nginx.pid 后来发现,kong的pid信息存放在/usr/local/kong/pids/nginx.pid kill -USR1 在这里指重新加载配置信息,重新建立对日志文件的连接 详情 man kill/kill -l 或者查看 Linux kill 文档 [Linux signal 文档](http://www.man7.org/linux/man-pages/man7/signal.7.html
包装 elastisearch API接口,简化经网关转发提供给外部使用Elastisearch 官网开放API 文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
获取某一时间 ddj 服务的访问次数 GET /_count { "query": { "bool":{ "filter": { "range": { "@timestamp": { "gte": "now-100h/h", "lte": "now/h", "format": "epoch_millis" } } } , "must": { "match": { "request": { "query": "*ddj*" } } } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
获取某一时间 ddj 服务的访问信息,相比于上者更为详细 GET /_search { "query": { "bool":{ "filter": { "range": { "@timestamp": { "gte": "now-100h/h", "lte": "now/h", "format": "epoch_millis" } } } , "must": { "match": { "request": { "query": "*ddj*" } } } } },"aggs": { "dateAgg": { "date_histogram": { "field": "@timestamp", "time_zone": "Europe/Amsterdam", "interval": "1m", "min_doc_count": 1 } } } }
本来想自己开发一个插件,似乎Kong Hub 上提供的第三方插件 Template Transformer 可以满足需要,源码 可以观摩下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
curl -X POST http://localhost:8001/routes/4f240596-9f55-4b90-a2be-2329be72391e/plugins --data "name=kong-plugin-template-transformer" --data "config.request_template=' { "query": { "bool": { "filter": { "range": { "@timestamp": { "gte": "now-{{headers.gt}}h/h", "lte": "now/h", "format": "epoch_millis" } } } , "must": { "match": { "request": { "query": "*{{headers.department}}*" } } } } } }" {"query":{"bool":{"filter": {"range": {"@timestamp":{"gte": "now-{{headers.gt}}h/h","lte": "now/h","format": "epoch_millis"}}}}},"size":0,"aggs":{"test":{"terms":{ "field": "user.keyword","size":{{headers.size}}}}}}" /api/v1/data/es/list_user curl -X POST http://localhost:8001/routes/{route}/plugins --data "name=kong-plugin-template-transformer" --data "config.request_template={ \"query\": { \"bool\": { \"filter\": { \"range\": { \"@timestamp\": { \"gte\": \"now-{{headers.gt}}h/h\", \"lte\": \"now/h\", \"format\": \"epoch_millis\" } } } } }, \"size\":0, \"aggs\": { \"test\":{ \"terms\":{ \"field\": \"user.keyword\", \"size\":{{headers.size}} } } } }" /api\/v1\/data\/es\/history curl -X POST http://localhost:8001/routes/{route}/plugins --data "name=kong-plugin-template-transformer" --data "config.request_template={ \"size\": {{headers.size}}, \"query\": { \"bool\": { \"must\": [ { \"match\": { \"user\": \"{{headers.name}}\" } }, { \"match\": { \"_index\":\"system-syslog*\" } }] } }, \"_source\": { \"includes\": [\"@timestamp\",\"referer\",\"request\",\"user\"] }, \"sort\": [ {\"@timestamp\":{\"order\":\"desc\" ,\"unmapped_type\":\"date\" } }, {\"_score\":{\"order\":\"asc\" } } ] }"
2019-9-24 更改为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
curl -i -X POST --url http://localhost:8001/services/-search/routes --data 'paths=/api/v1/data/es/history' curl -X POST http://localhost:8001/routes/{route_id}/plugins --data "name=kong-plugin-template-transformer" --data "config.request_template={ \"query\": { \"bool\": { \"filter\": { \"range\": { \"@timestamp\": { \"gte\": \"now-{{headers.gt}}h/h\", \"lte\": \"now/h\", \"format\": \"epoch_millis\" } } } , \"must\": { \"match\": { \"request\": { \"query\": \"*{{headers.department}}*\" } } } } } }"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
curl -i -X POST --url http://localhost:8001/services/-search/routes --data 'paths=/api/v1/data/es/history' { \"size\": {{headers.size}}, \"query\": { \"bool\": { \"must\": [ { \"match\": { \"user\": \"{{headers.name}}\" } }, { \"match\": { \"_index\":\"system-syslog*\" } } ] } }, \"_source\": { \"includes\": [ \"@timestamp\", \"referer\", \"request\", \"user\" ] }, \"sort\": [ {\"@timestamp\":{\"order\":\"desc\" ,\"unmapped_type\":\"date\" } }, {\"_score\":{\"order\":\"asc\" } } ] }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
curl -i -X POST --url http://localhost:8001/services/-search/routes --data 'paths=/api/v1/data/es/list_user' curl -X POST http://localhost:8001/routes/ad0226f7-0877-4557-b81b-f30cd227cf61/plugins --data "name=kong-plugin-template-transformer" --data "config.request_template={ \"query\": { \"bool\": { \"filter\": { \"range\": { \"@timestamp\": { \"gte\": \"now-{{headers.gt}}h/h\", \"lte\": \"now/h\", \"format\": \"epoch_millis\" } } } } }, \"size\":0, \"aggs\": { \"test\": { \"terms\": { \"field\": \"user.keyword\", \"size\":{{headers.size}} } } } }"
luarocks make luarocks pack kong-plugin-myplugin 0.1.0-1 luarocks install kong-plugin-myplugin-0.1.0-1.all.rock kong restart
转载于:https://www.whetstone.life/2019/05/15/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/%E8%BF%90%E7%BB%B4/