My slstatus configuration
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.
 
 
 
 

136 lines
2.7 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include "../util.h"
  5. #if defined(__linux__)
  6. #include <stdint.h>
  7. const char *
  8. netspeed_rx(const char *interface)
  9. {
  10. uint64_t oldrxbytes;
  11. static uint64_t rxbytes;
  12. extern const unsigned int interval;
  13. char path[PATH_MAX];
  14. oldrxbytes = rxbytes;
  15. if (esnprintf(path, sizeof(path),
  16. "/sys/class/net/%s/statistics/rx_bytes",
  17. interface) < 0) {
  18. return NULL;
  19. }
  20. if (pscanf(path, "%llu", &rxbytes) != 1) {
  21. return NULL;
  22. }
  23. if (oldrxbytes == 0) {
  24. return NULL;
  25. }
  26. return fmt_human_2((rxbytes - oldrxbytes) * 1000 / interval);
  27. }
  28. const char *
  29. netspeed_tx(const char *interface)
  30. {
  31. uint64_t oldtxbytes;
  32. static uint64_t txbytes;
  33. extern const unsigned int interval;
  34. char path[PATH_MAX];
  35. oldtxbytes = txbytes;
  36. if (esnprintf(path, sizeof(path),
  37. "/sys/class/net/%s/statistics/tx_bytes",
  38. interface) < 0) {
  39. return NULL;
  40. }
  41. if (pscanf(path, "%llu", &txbytes) != 1) {
  42. return NULL;
  43. }
  44. if (oldtxbytes == 0) {
  45. return NULL;
  46. }
  47. return fmt_human_2((txbytes - oldtxbytes) * 1000 / interval);
  48. }
  49. #elif defined(__OpenBSD__)
  50. #include <string.h>
  51. #include <ifaddrs.h>
  52. #include <sys/types.h>
  53. #include <sys/socket.h>
  54. #include <net/if.h>
  55. const char *
  56. netspeed_rx(const char *interface)
  57. {
  58. struct ifaddrs *ifal, *ifa;
  59. struct if_data *ifd;
  60. uint64_t oldrxbytes;
  61. static uint64_t rxbytes;
  62. extern const unsigned int interval;
  63. int if_ok = 0;
  64. oldrxbytes = rxbytes;
  65. if (getifaddrs(&ifal) == -1) {
  66. warn("getifaddrs failed");
  67. return NULL;
  68. }
  69. rxbytes = 0;
  70. for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
  71. if (!strcmp(ifa->ifa_name, interface) &&
  72. (ifd = (struct if_data *)ifa->ifa_data)) {
  73. rxbytes += ifd->ifi_ibytes, if_ok = 1;
  74. }
  75. }
  76. freeifaddrs(ifal);
  77. if (!if_ok) {
  78. warn("reading 'if_data' failed");
  79. return NULL;
  80. }
  81. if (oldrxbytes == 0) {
  82. return NULL;
  83. }
  84. return fmt_human_2((rxbytes - oldrxbytes) * 1000 / interval);
  85. }
  86. const char *
  87. netspeed_tx(const char *interface)
  88. {
  89. struct ifaddrs *ifal, *ifa;
  90. struct if_data *ifd;
  91. uint64_t oldtxbytes;
  92. static uint64_t txbytes;
  93. extern const unsigned int interval;
  94. int if_ok = 0;
  95. oldtxbytes = txbytes;
  96. if (getifaddrs(&ifal) == -1) {
  97. warn("getifaddrs failed");
  98. return NULL;
  99. }
  100. txbytes = 0;
  101. for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
  102. if (!strcmp(ifa->ifa_name, interface) &&
  103. (ifd = (struct if_data *)ifa->ifa_data)) {
  104. txbytes += ifd->ifi_obytes, if_ok = 1;
  105. }
  106. }
  107. freeifaddrs(ifal);
  108. if (!if_ok) {
  109. warn("reading 'if_data' failed");
  110. return NULL;
  111. }
  112. if (oldtxbytes == 0) {
  113. return NULL;
  114. }
  115. return fmt_human_2((txbytes - oldtxbytes) * 1000 / interval);
  116. }
  117. #endif