A clone of btpd with my configuration changes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.1 KiB

  1. #include "btcli.h"
  2. void
  3. usage_stat(void)
  4. {
  5. printf(
  6. "Display stats for active torrents.\n"
  7. "\n"
  8. "Usage: stat [-i] [-w seconds] [torrent ...]\n"
  9. "\n"
  10. "Arguments:\n"
  11. "torrent ...\n"
  12. "\tOnly display stats for the given torrents.\n"
  13. "\n"
  14. "Options:\n"
  15. "-i\n"
  16. "\tDisplay individual lines for each torrent.\n"
  17. "\n"
  18. "-n\n"
  19. "\tDisplay the name of each torrent. Implies '-i'.\n"
  20. "\n"
  21. "-w n\n"
  22. "\tDisplay stats every n seconds.\n"
  23. "\n");
  24. exit(1);
  25. }
  26. struct btstat {
  27. unsigned num;
  28. enum ipc_tstate state;
  29. unsigned peers, tr_errors;
  30. long long content_got, content_size, downloaded, uploaded, rate_up,
  31. rate_down, tot_up;
  32. uint32_t pieces_seen, torrent_pieces;
  33. };
  34. struct cbarg {
  35. int individual, names;
  36. struct btstat tot;
  37. };
  38. static enum ipc_tval stkeys[] = {
  39. IPC_TVAL_STATE,
  40. IPC_TVAL_NUM,
  41. IPC_TVAL_NAME,
  42. IPC_TVAL_PCOUNT,
  43. IPC_TVAL_TRERR,
  44. IPC_TVAL_PCCOUNT,
  45. IPC_TVAL_PCSEEN,
  46. IPC_TVAL_SESSUP,
  47. IPC_TVAL_SESSDWN,
  48. IPC_TVAL_TOTUP,
  49. IPC_TVAL_RATEUP,
  50. IPC_TVAL_RATEDWN,
  51. IPC_TVAL_CGOT,
  52. IPC_TVAL_CSIZE
  53. };
  54. static size_t nstkeys = sizeof(stkeys) / sizeof(stkeys[0]);
  55. static void
  56. print_stat(struct btstat *st)
  57. {
  58. print_percent(st->content_got, st->content_size);
  59. print_size(st->downloaded);
  60. print_rate(st->rate_down);
  61. print_size(st->uploaded);
  62. print_rate(st->rate_up);
  63. print_ratio(st->tot_up, st->content_size);
  64. printf("%4u ", st->peers);
  65. print_percent(st->pieces_seen, st->torrent_pieces);
  66. if (st->tr_errors > 0)
  67. printf("E%u", st->tr_errors);
  68. printf("\n");
  69. }
  70. static void
  71. stat_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
  72. {
  73. struct cbarg *cba = arg;
  74. struct btstat st, *tot = &cba->tot;
  75. if (objerr != IPC_OK || res[IPC_TVAL_STATE].v.num == IPC_TSTATE_INACTIVE)
  76. return;
  77. bzero(&st, sizeof(st));
  78. st.state = res[IPC_TVAL_STATE].v.num;
  79. st.num = res[IPC_TVAL_NUM].v.num;
  80. tot->torrent_pieces += (st.torrent_pieces = res[IPC_TVAL_PCCOUNT].v.num);
  81. tot->pieces_seen += (st.pieces_seen = res[IPC_TVAL_PCSEEN].v.num);
  82. tot->content_got += (st.content_got = res[IPC_TVAL_CGOT].v.num);
  83. tot->content_size += (st.content_size = res[IPC_TVAL_CSIZE].v.num);
  84. tot->downloaded += (st.downloaded = res[IPC_TVAL_SESSDWN].v.num);
  85. tot->uploaded += (st.uploaded = res[IPC_TVAL_SESSUP].v.num);
  86. tot->rate_down += (st.rate_down = res[IPC_TVAL_RATEDWN].v.num);
  87. tot->rate_up += (st.rate_up = res[IPC_TVAL_RATEUP].v.num);
  88. tot->peers += (st.peers = res[IPC_TVAL_PCOUNT].v.num);
  89. tot->tot_up += (st.tot_up = res[IPC_TVAL_TOTUP].v.num);
  90. if ((st.tr_errors = res[IPC_TVAL_TRERR].v.num) > 0)
  91. tot->tr_errors++;
  92. if (cba->individual) {
  93. if (cba->names)
  94. printf("%.*s\n", (int)res[IPC_TVAL_NAME].v.str.l,
  95. res[IPC_TVAL_NAME].v.str.p);
  96. printf("%4u %c. ", st.num, tstate_char(st.state));
  97. print_stat(&st);
  98. }
  99. }
  100. static void
  101. do_stat(int individual, int names, int seconds, struct ipc_torrent *tps,
  102. int ntps)
  103. {
  104. enum ipc_err err;
  105. struct cbarg cba;
  106. int header = 1;
  107. if (names)
  108. individual = 1;
  109. cba.individual = individual;
  110. cba.names = names;
  111. again:
  112. header--;
  113. if (header == 0) {
  114. if (individual) {
  115. header = 1;
  116. printf(" NUM ST ");
  117. } else
  118. header = 20;
  119. printf(" HAVE DLOAD RTDWN ULOAD RTUP RATIO CONN"
  120. " AVAIL\n");
  121. }
  122. bzero(&cba.tot, sizeof(cba.tot));
  123. cba.tot.state = IPC_TSTATE_INACTIVE;
  124. if (tps == NULL)
  125. err = btpd_tget_wc(ipc, IPC_TWC_ACTIVE, stkeys, nstkeys,
  126. stat_cb, &cba);
  127. else
  128. err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba);
  129. if (err != IPC_OK)
  130. errx(1, ipc_strerror(err));
  131. if (names)
  132. printf("-------\n");
  133. if (individual)
  134. printf(" ");
  135. print_stat(&cba.tot);
  136. if (seconds > 0) {
  137. sleep(seconds);
  138. goto again;
  139. }
  140. }
  141. static struct option stat_opts [] = {
  142. { "help", no_argument, NULL, 'H' },
  143. {NULL, 0, NULL, 0}
  144. };
  145. void
  146. cmd_stat(int argc, char **argv)
  147. {
  148. int ch;
  149. int wflag = 0, iflag = 0, nflag = 0, seconds = 0;
  150. struct ipc_torrent *tps = NULL;
  151. int ntps = 0;
  152. char *endptr;
  153. while ((ch = getopt_long(argc, argv, "inw:", stat_opts, NULL)) != -1) {
  154. switch (ch) {
  155. case 'i':
  156. iflag = 1;
  157. break;
  158. case 'n':
  159. nflag = 1;
  160. break;
  161. case 'w':
  162. wflag = 1;
  163. seconds = strtol(optarg, &endptr, 10);
  164. if (*endptr != '\0' || seconds < 1)
  165. usage_stat();
  166. break;
  167. default:
  168. usage_stat();
  169. }
  170. }
  171. argc -= optind;
  172. argv += optind;
  173. if (argc > 0) {
  174. tps = malloc(argc * sizeof(*tps));
  175. for (int i = 0; i < argc; i++) {
  176. if (torrent_spec(argv[i], &tps[ntps]))
  177. ntps++;
  178. else
  179. exit(1);
  180. }
  181. }
  182. btpd_connect();
  183. do_stat(iflag, nflag, seconds, tps, ntps);
  184. }