ActiveResource与另一台具有速率限制的服务器通信
连接.我很好奇我如何能够从我的web应用程序运行的主机上最好地监控 – 也就是说,从我的服务器上的 linux上的bash提示符,我如何测量我的机器每秒向另一台机器发出的出站请求?
我正在寻找一个带有接口,主机名的linux one-liner
和/或它们的某种组合,告诉我我的连接率
制作到那台服务器.我已经接近了像tc和iftop这样的工具,但那些报告了传输的数据量,而不是所做的连接…所以它不是我想要的.我很想看到一些东西
喜欢:
$awesometool –host thetargethost.com –interface eth0 –interval 5
收集统计数据
通过接口eth0,thetargethost.com的每秒请求报告
平均:23 req / sec min:12 req / sec max 39 req / sec
取5个样品
谁能指点我一个人?
解决方法 tcpdump(8)可以提供非常相似的东西;搜索TCP数据包,并将SYN标志设置为捕获 three-way handshake中发往其他对等方的第一个数据包:$sudo tcpdump -c 10 -i eth0 "tcp[tcpflags] & (tcp-syn) != 0 and dst 192.168.0.1"tcpdump: verbose output suppressed,use -v or -vv for full protocol decodeListening on eth0,link-type EN10MB (Ethernet),capture size 65535 bytes18:26:24.800308 IP haig.59419 > 192.168.0.1.telnet: Flags [S],seq 3197302320,win 14600,options [mss 1460,sackOK,TS val 19460844 ecr 0,nop,wscale 7],length 0...18:26:27.420132 IP haig.59428 > 192.168.0.1.telnet: Flags [S],seq 1238498237,TS val 19461106 ecr 0,length 010 packets captured10 packets received by filter0 packets dropped by kernel
您可以使用/usr/bin/time或内置shell的时间,或者对输出中的时间戳进行一些算术运算,以获得每秒的平均速率. (使用十个以上的数据包 – 这只是为了演示.)
更新
我写了一个小程序来运行tcpdump(8),计算数据包,并报告在指定的时间间隔内发送了多少数据包:
# ./counter --host 192.168.0.1 --interface eth0 --interval 32 requests in 3 seconds; average 0.67 req/seq20 requests in 3 seconds; average 6.67 req/seq19 requests in 3 seconds; average 6.33 req/seq19 requests in 3 seconds; average 6.33 req/seq^C# ./counter --host 192.168.0.1 --interface eth0 --interval 530 requests in 5 seconds; average 6.00 req/seq20 requests in 5 seconds; average 4.00 req/seq1176 requests in 5 seconds; average 235.20 req/seq1414 requests in 5 seconds; average 282.80 req/seq0 requests in 5 seconds; average 0.00 req/seq^C
因为它要求tcpdump(8)使用行缓冲输出,我有点害怕它可能无法超过每秒200-300个请求,至少在我的硬件上.但是没有行缓冲输出,tcpdump(8)会等到它的输出缓冲区(详见setvbuf(3))在发送任何输出之前都已满,导致极其抖动的结果.
但如果您的连接速度不是那么高,那么这可能会满足您的需求.如果您的连接速率较高,那么这个小小的黑客可能最好被忽略 – 这让我觉得可能iptables(8)可以计算流.
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <getopt.h>#include <signal.h>#include <errno.h>#include <fcntl.h>#include <sys/types.h>#define CMDLEN 1024#define TCPDUMPLEN 4096int show_stats;long counter;voID alarm_handler(int signum){ show_stats = 1;}voID install_handler(voID){ struct sigaction sa; memset(&sa,sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = &alarm_handler; if (sigaction(SIgalRM,&sa,NulL) == -1) { perror("Can't install alarm handler"); exit(1); }}int count_lines(char *p,int bytes){ int counter = 0; char *i; for (i=p; i < p+bytes ; i++) { if (*i == '\n') counter++; } return counter;}int spawn_tcpdump(char *host,char *interface){ int fd[2]; pID_t child; if (pipe(fd) == -1) { perror("Can't create pipes"); exit(1); } child = fork(); if (child == -1) { perror("Can't fork(2) for tcpdump"); exit(1); } if (child == 0) { int null; int len; char syn_and_dst[CMDLEN]; len = snprintf(syn_and_dst,CMDLEN,"tcp[tcpflags] & (tcp-syn) != 0 and dst %s",host); if (len > CMDLEN) { perror("host argument too long"); exit(1); } /* child writes into pipe */ close(fd[0]); dup2(fd[1],STDOUT_fileNO); /* throw away first two lines of tcpdump output */ null = open("/dev/null",O_WRONLY); if (null == -1) { perror("Can't open /dev/null"); exit(1); } dup2(null,STDERR_fileNO); execl("/usr/sbin/tcpdump","tcpdump","-l","-n","-s 96","-i",interface,syn_and_dst,(char *) NulL); /* can't reach */ perror("Cannot execute tcpdump"); exit(1); } else { /* parent reads from pipe */ close(fd[1]); return fd[0]; }}int main(int argc,char *argv[]){ int tcpdump; char *host; char *interface; long interval; while (1) { int option_index; int c; static struct option opts[] = { {"host",required_argument,NulL,'h'},{"interface",'i'},{"interval",'n'},{0,0},}; c = getopt_long(argc,argv,"",opts,&option_index); if (c == -1) break; switch (c) { case 'h': host = strdup(optarg); break; case 'i': interface = strdup(optarg); break; case 'n': { char *endptr; interval = strtol(optarg,&endptr,10); if (!(optarg[0] != '' && endptr[0] == '')) { fprintf(stderr,"Expected integer; invalID" " input '%s'\n",optarg); exit(1); } } break; default: fprintf(stderr,"Option parsing error\n"); exit(1); } } if (optind < argc) { fprintf(stderr,"unexpected arguments: "); while (optind < argc) { fprintf(stderr,"%s ",argv[optind++]); } fprintf(stderr,"\n"); } tcpdump = spawn_tcpdump(host,interface); install_handler(); alarm(interval); while(1) { if (show_stats) { printf("%ld requests in %ld seconds; average %2.2f req/seq\n",counter,interval,(double)counter / (double)interval); counter = 0; show_stats = 0; alarm(interval); } else { char buffer[TCPDUMPLEN]; int ret; memset(buffer,TCPDUMPLEN); ret = read(tcpdump,buffer,TCPDUMPLEN); if (ret == -1 && errno == EINTR) { /* nop */ } else if (ret == -1) { perror("read"); exit(1); } else { counter += count_lines(buffer,ret); } } } exit(0);}总结
以上是内存溢出为你收集整理的ruby – 每秒测量出站请求数?全部内容,希望文章能够帮你解决ruby – 每秒测量出站请求数?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)