gpssnmp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* gpssnmp - poll local gpsd for SNMP variables
  2. *
  3. * To build this:
  4. * gcc -o gpssnmp gpssnmp.c -lgps
  5. *
  6. * Copyright 2016 David Taylor <gpsd@david.taylor.name>
  7. *
  8. * Copyright 2018 by the GPSD project
  9. * SPDX-License-Identifier: BSD-2-clause
  10. *
  11. */
  12. #include "../include/gpsd_config.h" // must be before all includes
  13. #ifdef HAVE_GETOPT_LONG
  14. #include <getopt.h>
  15. #endif
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h> // for strlcpy()
  19. #include "../include/compiler.h" // for FALLTHROUGH
  20. #include "../include/gps.h"
  21. #include "../include/gpsdclient.h" // for gpsd_source_spec()
  22. #include "../include/os_compat.h" // for strlcpy() if needed
  23. #define OID_VISIBLE ".1.3.6.1.2.1.25.1.31"
  24. #define OID_USED ".1.3.6.1.2.1.25.1.32"
  25. #define OID_SNR_AVG ".1.3.6.1.2.1.25.1.33"
  26. static void usage(char *prog_name) {
  27. // "%s [-h] [-g OID] [server[:port[:device]]]\n\n"
  28. printf("Usage:\n"
  29. "%s [-h] [-g OID]\n\n"
  30. "Examples:\n"
  31. "to get OID_VISIBLE\n"
  32. " $ gpssnmp -g .1.3.6.1.2.1.25.1.31\n"
  33. " .1.3.6.1.2.1.25.1.31\n"
  34. " gauge\n"
  35. " 13\n\n"
  36. "to get OID_USED\n"
  37. " $ gpssnmp -g .1.3.6.1.2.1.25.1.32\n"
  38. " .1.3.6.1.2.1.25.1.32\n"
  39. " gauge\n"
  40. " 4\n\n"
  41. "to get OID_SNR_AVG\n"
  42. " $ gpssnmp -g .1.3.6.1.2.1.25.1.33\n"
  43. " .1.3.6.1.2.1.25.1.33\n"
  44. " gauge\n"
  45. " 22.250000\n\n", prog_name);
  46. }
  47. int main (int argc, char **argv)
  48. {
  49. struct gps_data_t gpsdata;
  50. int i;
  51. double snr_total=0;
  52. double snr_avg = 0.0;
  53. int status, used, visible;
  54. char oid[30] = ""; // requested OID
  55. int debug = 0;
  56. struct fixsource_t source;
  57. const char *optstring = "?D:g:hV";
  58. #ifdef HAVE_GETOPT_LONG
  59. int option_index = 0;
  60. static struct option long_options[] = {
  61. {"debug", required_argument, NULL, 'D'},
  62. {"help", no_argument, NULL, 'h'},
  63. {"version", no_argument, NULL, 'V' },
  64. {NULL, 0, NULL, 0},
  65. };
  66. #endif // HAVE_GETOPT_LONG
  67. /* Process the options. Print help if requested. */
  68. while (1) {
  69. int ch;
  70. #ifdef HAVE_GETOPT_LONG
  71. ch = getopt_long(argc, argv, optstring, long_options, &option_index);
  72. #else
  73. ch = getopt(argc, argv, optstring);
  74. #endif
  75. if (ch == -1) {
  76. break;
  77. }
  78. switch (ch) {
  79. case '?':
  80. FALLTHROUGH
  81. case 'h':
  82. usage(argv[0]);
  83. exit(0);
  84. break;
  85. case 'g':
  86. strlcpy(oid, optarg, sizeof(oid));
  87. break;
  88. case 'D':
  89. debug = atoi(optarg);
  90. gps_enable_debug(debug, stderr);
  91. break;
  92. case 'V':
  93. (void)fprintf(stderr, "%s: %s (revision %s)\n",
  94. argv[0], VERSION, REVISION);
  95. exit(EXIT_SUCCESS);
  96. default:
  97. usage(argv[0]);
  98. exit(0);
  99. break;
  100. }
  101. }
  102. if ('\0' == oid[0]) {
  103. (void)fprintf(stderr, "%s: ERROR: Missing option\n\n", argv[0]);
  104. usage(argv[0]);
  105. exit(1);
  106. }
  107. /* Grok the server, port, and device. */
  108. if (optind < argc) {
  109. gpsd_source_spec(argv[optind], &source);
  110. } else {
  111. gpsd_source_spec(NULL, &source);
  112. }
  113. /* Open the stream to gpsd. */
  114. // broken, used shared memory.
  115. // status = gps_open(source.server, source.port, &gpsdata);
  116. status = gps_open(GPSD_SHARED_MEMORY, DEFAULT_GPSD_PORT, &gpsdata);
  117. if (0 != status) {
  118. (void)fprintf(stderr, "gpssnmp: ERROR: connection failed\n");
  119. exit(1);
  120. }
  121. status = gps_read(&gpsdata, NULL, 0);
  122. if (-1 == status) {
  123. (void)fprintf(stderr, "gpssnmp: ERROR: read failed %d\n", status);
  124. exit(1);
  125. }
  126. used = gpsdata.satellites_used;
  127. visible = gpsdata.satellites_visible;
  128. for(i = 0; i <= used; i++) {
  129. if (0 < gpsdata.skyview[i].used &&
  130. 1 < gpsdata.skyview[i].ss) {
  131. // printf("i: %d, P:%d, ss: %f\n", i, gpsdata.skyview[i].PRN,
  132. // gpsdata.skyview[i].ss);
  133. snr_total+=gpsdata.skyview[i].ss;
  134. }
  135. }
  136. gps_close (&gpsdata);
  137. if (0 < used) {
  138. snr_avg = snr_total / used;
  139. }
  140. if (strcmp(OID_VISIBLE, oid) == 0) {
  141. printf(OID_VISIBLE);
  142. printf("\ngauge\n%d\n", visible);
  143. } else if (strcmp(OID_USED, oid) == 0) {
  144. printf(OID_USED);
  145. printf("\ngauge\n%d\n", used);
  146. } else if (strcmp(OID_SNR_AVG, oid) == 0) {
  147. printf(OID_SNR_AVG);
  148. printf("\ngauge\n%lf\n", snr_avg);
  149. } else {
  150. (void)fprintf(stderr, "%s: ERROR: Unknown OID %s\n\n",
  151. argv[0], oid);
  152. usage(argv[0]);
  153. exit(1);
  154. }
  155. return 0;
  156. }