/usr/sbin/ntploggps is in ntpsec-ntpviz 1.1.0+dfsg1-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | #!/usr/bin/python
# -*- coding: utf-8 -*-
"""\
usage: ntploggps [-h] [-o] [-l LOGFILE] [-v] [-V]
gpsd log file generator
optional arguments:
-h, --help show this help message and exit
-l LOGFILE, --logfile LOGFILE
append log data to LOGFILE instead of stdout
-o, --once log one line, then exit
-w WAIT, --wait WAIT wait WAIT seconds after each log line, default 5
-v, --verbose be verbose
-V, --version show program's version number and exit
See the manual page for details.
"""
from __future__ import print_function
import io
import logging
import logging.handlers
import sys
import threading
import time
try:
import argparse
except ImportError:
sys.stderr.write("""
ntploggps: can't find the Python argparse module
If your Python version is < 2.7, then manual installation is needed:
# pip install argparse
""")
sys.exit(1)
try:
import gps
except ImportError as e:
sys.stderr.write("ntploggps: can't find Python GPSD library.\n")
sys.stderr.write("%s\n" % e)
sys.exit(1)
def logging_setup():
"Create logging object"
logFormat = logging.Formatter('%(message)s')
# Create logger for gpsd
Logger = logging.getLogger()
Logger.setLevel(logging.INFO)
# Create file handler
if args.logfile:
# log to logfile
file = logging.handlers.TimedRotatingFileHandler(
args.logfile[0],
when='midnight',
interval=1)
else:
# log to stdout
file = logging.StreamHandler(sys.stdout)
file.setLevel(logging.INFO)
# Create the formatter and add it to the handler
file.setFormatter(logFormat)
# Add the handler to the logger
Logger.addHandler(file)
return Logger
parser = argparse.ArgumentParser(description="gpsd log file generator",
epilog="""
See the manual page for details.
""")
parser.add_argument('-l', '--logfile',
dest='logfile',
help="append log data to LOGFILE instead of stdout",
nargs=1)
parser.add_argument('-o', '--once',
action="store_true",
dest='once',
help="log one line, then exit")
parser.add_argument('-w', '--wait',
default=[5],
dest='wait',
help="wait WAIT seconds after each log line, default 5",
nargs=1,
type=int)
parser.add_argument('-v', '--verbose',
action="store_true",
dest='verbose',
help="be verbose")
args = parser.parse_args()
if args.verbose:
print("ntploggps: arguments:")
print(args)
if args.logfile:
# log to logfile
try:
out = open(args.logfile[0], mode='a')
except io.UnsupportedOperation as e:
sys.stderr.write("ntploggps: can't open logfile %s\n" % args.logfile)
sys.stderr.write("%s\n" % e)
sys.exit(1)
if args.verbose:
print("ntploggps: opened log file %s" % args.logfile[0])
else:
# log to stdout
out = sys.stdout
class GpsPoller(threading.Thread):
running = False # True when thread is running. Quit when set False
def __init__(self):
threading.Thread.__init__(self)
self.device = None
self.satellites_used = None
self.tdop = None
# start the streaming of gps data
try:
self.gpsd = gps.gps(mode=gps.WATCH_ENABLE)
except BaseException as e:
sys.stderr.write("ntploggps: Can't connect to gpsd, %s\n"
" Is gpsd running?\n" % e)
sys.exit(1)
self.running = True
def run(self):
while gpsp.running:
if self.gpsd.read() == -1:
self.running = False
break
if hasattr(self.gpsd, "data"):
if self.gpsd.data.get("class") == "SKY":
self.satellites_used = 0
self.tdop = self.gpsd.data.get("tdop", 0)
for sat in self.gpsd.data.get("satellites", []):
if sat["used"]:
self.satellites_used += 1
elif self.gpsd.data.get("class") == "TPV":
self.device = self.gpsd.data.get("device")
@property
def time(self):
"Return the gpsd time fix"
t = self.gpsd.fix.time
if isinstance(t, int):
return t
if isinstance(t, float):
if gps.isnan(t):
return None
return t
return gps.isotime(t)
if __name__ == '__main__':
# this is the main thread
if args.verbose:
print("ntploggps: creating poll thread")
gpsp = GpsPoller() # create the thread
try:
# Create the logger instance
Logger = logging_setup()
# Create data layout
Logger.info("# Time Device TDOP nSat")
gpsp.start() # start it up
last_time = 0
while gpsp.running:
# It may take a second or two to get good data
try:
current_time = gpsp.time
device = gpsp.device
tdop = gpsp.tdop
satellites_used = gpsp.satellites_used
if current_time is not None and \
device is not None and \
satellites_used is not None and \
tdop is not None:
if last_time != current_time:
s = '%i %s %f %d' % (current_time, device, tdop,
satellites_used)
Logger.info(s)
last_time = current_time
if args.once:
# just once
break
except AttributeError as e:
print('parse error\n')
# wait a bit before next log
time.sleep(args.wait[0])
except (KeyboardInterrupt, SystemExit): # when you press ctrl+c
args.once = True # stop the retry loop
if args.verbose:
print("\nKilling Thread...")
else:
# print a blank line to make bash happy
print("")
except Exception as e: # any error, signal
print(e)
# tell the thread to die
gpsp.running = False
# wait for the thread to finish what it's doing
gpsp.join()
if args.verbose:
print("ntploggps: Done -- Exiting.")
|