全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 862|回复: 10
打印 上一主题 下一主题

[Windows VPS] 技术大佬请进

[复制链接]
跳转到指定楼层
1#
发表于 2019-6-6 10:51:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
请教大佬一个问题哈。跟论坛大佬要了一份Lightsail超量关机的python代码
在用的时候 有一点疑问。烦请知道的大佬帮忙解答一下,感谢

代码如下:
  1. #coding=utf-8

  2. limit_total=0# limit_total 上传+下载的流量限制,单位GB,如果不限制就是0,如果限制1T就是1024
  3. limit_in=0# limit_in 下载的流量限制,单位GB,如果不限制就是0,如果限制1T就是1024
  4. limit_out=0# limit_out 上传的流量限制,单位GB,如果不限制就是0,如果限制1T就是1024
  5. sleep=60#多久检查一次,单位是秒


  6. import os
  7. import time
  8. NET_IN = 0
  9. NET_OUT = 0

  10. while True:
  11.         vnstat=os.popen('vnstat --dumpdb').readlines()
  12.         for line in vnstat:
  13.                 if line[0:4] == "m;0;":
  14.                         mdata=line.split(";")
  15.                         NET_IN=int(mdata[3])/1024
  16.                         NET_OUT=int(mdata[4])/1024
  17.                         break

  18.         killssr="`ps aux|grep server.py|awk '{print "kill "$2}'`"
  19.         if (limit_total != 0 and (NET_IN+NET_OUT)>=limit_total):
  20.                 os.system(killssr)
  21.                 break
  22.         elif (limit_in != 0 and NET_IN>=limit_in):
  23.                 os.system(killssr)
  24.                 break
  25.         elif (limit_out != 0 and NET_OUT>=limit_out):
  26.                 os.system(killssr)
  27.                 break

  28.         time.sleep(sleep)
复制代码


我测试的时候:

  1. vnstat --dumpdb
  2. #反馈
  3. m;0;1559785144;0;0;198;268;1
复制代码


那么根据代码里面的意思
  1. NET_IN=int(mdata[3])/1024  # 这个mdata[3] 是不是反馈数据:“m;0;1559785144;0;0;198;268;1 ”的 第四位,也就是0
  2.                         NET_OUT=int(mdata[4])/1024   # 这个mdata[4] 是不是反馈数据:“m;0;1559785144;0;0;198;268;1 ” 的第5位,也是0.
复制代码


但是我VPS已经下载了很多文件包,还用军哥的lnmp包安装了一个环境,按理流入流量应该不会是0啊?
不知道我的表达是不是清晰。
2#
发表于 2019-6-6 10:52:26 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
3#
发表于 2019-6-6 11:01:20 来自手机 | 只看该作者
我建议你把aws的账单用上
4#
 楼主| 发表于 2019-6-6 11:03:20 | 只看该作者
h2o 发表于 2019-6-6 10:52
太麻烦,直接ifconfig 取流量 然后poweroff也就几行的shell脚本

  大佬能不能飚几行代码。小弟参考一下  
5#
 楼主| 发表于 2019-6-6 11:12:44 | 只看该作者
Kslr 发表于 2019-6-6 11:01
我建议你把aws的账单用上

大佬,这个是什么操作,请教一下。要不是撸了amz的码子 真心的不像用他们家的 太麻烦了。
6#
发表于 2019-6-6 11:15:29 来自手机 | 只看该作者
xiaoxiaobai 发表于 2019-6-6 11:12
大佬,这个是什么操作,请教一下。要不是撸了amz的码子 真心的不像用他们家的 太麻烦了。 ...

找一找成本管理
7#
 楼主| 发表于 2019-6-6 11:18:45 | 只看该作者
Kslr 发表于 2019-6-6 11:15
找一找成本管理

成本管理我制定了,但是达到我指定的成本后会自动停机吗?
8#
 楼主| 发表于 2019-6-6 11:21:52 | 只看该作者
Kslr 发表于 2019-6-6 11:15
找一找成本管理

大佬 你说的是不是这个啊? AWS Budgets 预算制定, 成本管理 我看好像是统计图来着?还是我没看对地方?
9#
发表于 2019-6-6 11:39:42 | 只看该作者
本帖最后由 coxyblog 于 2019-6-6 11:40 编辑

也不知道你用的哪个版本,查了下更新日志
2.0 / 21-Oct-2018

- Fixed
   - Data rate calculations when using SI decimal prefixes
   - Workaround for filled arcs not drawn correctly with some versions
     of libgd (libgd/libgd#351)
- New
   - C struct dump database has been replaced with sqlite database,
     full data import included from legacy databases of versions 1.3 - 1.18
   - User configurable data retention durations for all resolutions with
     following changed defaults:
                   v1.18     v2.0
       5 minutes     n/a      576   (48 hours)
       hours          24       96   (4 days)
       days           30       62   (2 full months)
       months         12       25   (2 full years)
       years         n/a      all
       top days       10       20
   - Option to select units to use SI decimal prefixes using calculations
     with powers of 1000 (IEC and JEDEC prefixes use powers of 1024)
   - Yearly and 5 minute resolution statistics
   - Pebibyte and exbibyte support
   - Options the configure the length of each list output
   - Option to limit the length of --json and --xml outputs
   - Hourly list output replaces the hourly graph when the -h parameter is used
   - Hourly graph is accessible with -hg / --hoursgraph parameter
   - Options -b / --begin and -e / --end for specifying query time range
   - Configuration comes with ISO 8601 date formats as new defaults
- Removed
   - Weekly output
   - Merge of data from multiple interfaces to one output
   - --testkernel
   - --exportdb, use --oneline, --json or --xml instead
   - --importdb, -u / --update and related options using vnstat command
   - Old style (default in versions up to 1.7) summary layout in image output
dumpdb之前更名为exportdb,现在exportdb也去掉了,用--oneline --json --xml代替了,然后给你看了下
--oneline [mode]
    Show traffic summary for selected interface using one line with a parsable format. The output contains 15 fields with ; used as field delimiter. The 1st field contains the API version information of the output that will only be changed in future versions if the field content or structure changes. The following fields in order 2) interface name, 3) timestamp for today, 4) rx for today, 5) tx for today, 6) total for today, 7) average traffic rate for today, 8) timestamp for current month, 9) rx for current month, 10) tx for current month, 11) total for current month, 12) average traffic rate for current month, 13) all time total rx, 14) all time total tx, 15) all time total traffic. An optional mode parameter can be used to force all fields to output in bytes without the unit itself shown.
自己从里面找找测试测试吧。
PS,用软件就得看人家官方的文档,不看说明书可是不行的。
10#
 楼主| 发表于 2019-6-6 12:10:48 | 只看该作者
coxyblog 发表于 2019-6-6 11:39
也不知道你用的哪个版本,查了下更新日志
2.0 / 21-Oct-2018

大佬,请教一下。vnstat是不是只能统计安装它之后的流量啊?我现在就是统计的数据不对。
  1. vnstat -m

  2. eth0  /  monthly

  3.        month        rx      |     tx      |    total    |   avg. rate
  4.     ------------------------+-------------+-------------+---------------
  5.       Jun '19       325 KiB |     414 KiB |     739 KiB |    0.01 kbit/s
  6.     ------------------------+-------------+-------------+---------------
  7.     estimated        --     |      --     |      --     |
复制代码

获取月度流量 竟然只有这么一点点。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-6-13 18:14 , Processed in 0.066939 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表