| 1 | #!/usr/bin/python
|
|---|
| 2 | # -*- coding: utf-8 -*-
|
|---|
| 3 | '''
|
|---|
| 4 | Copyright (C) 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 5 |
|
|---|
| 6 | This program is free software: you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 9 | (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | This program is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 | '''
|
|---|
| 19 |
|
|---|
| 20 | import rrdtool
|
|---|
| 21 | import argparse
|
|---|
| 22 | import random
|
|---|
| 23 | import time
|
|---|
| 24 | import sys
|
|---|
| 25 | import os
|
|---|
| 26 |
|
|---|
| 27 | from gnuboat import daemon
|
|---|
| 28 |
|
|---|
| 29 | # default values for command line arguments
|
|---|
| 30 | rrdfile = "/var/lib/gnuboat/gnuboat.rrd"
|
|---|
| 31 | # in seconds
|
|---|
| 32 | logperiod = 2
|
|---|
| 33 | duration = 24 * 60 * 60
|
|---|
| 34 | latitude_min = -90
|
|---|
| 35 | latitude_max = 90
|
|---|
| 36 | longitude_min = -180
|
|---|
| 37 | longitude_max = 180
|
|---|
| 38 |
|
|---|
| 39 | def main(argv):
|
|---|
| 40 | global rrdfile
|
|---|
| 41 | global duration
|
|---|
| 42 | global latitude_min
|
|---|
| 43 | global latitude_max
|
|---|
| 44 | global longitude_min
|
|---|
| 45 | global longitude_max
|
|---|
| 46 | global logperiod
|
|---|
| 47 |
|
|---|
| 48 | parser = argparse.ArgumentParser(description='Generates test data for gnuboat webservice and fills the rrd database')
|
|---|
| 49 | parser.add_argument('-f', '--file', dest='rrd_database', metavar='rrd_database', type=str,
|
|---|
| 50 | help='File path of the rrd database to fill', default="/var/lib/gnuboat/gnuboat.rrd")
|
|---|
| 51 | parser.add_argument('-lat-min', '--latitude-min', dest='lat_min', metavar='lat_min', type=float,
|
|---|
| 52 | help='Minimum latitude generated', default=latitude_min)
|
|---|
| 53 | parser.add_argument('-lat-max', '--latitude-max', dest='lat_max', metavar='lat_max', type=float,
|
|---|
| 54 | help='Maximum latitude generated', default=latitude_max)
|
|---|
| 55 | parser.add_argument('-lon-min', '--longitude-min', dest='lon_min', metavar='lon_min', type=float,
|
|---|
| 56 | help='Minimum longitude generated', default=longitude_min)
|
|---|
| 57 | parser.add_argument('-lon-max', '--longitude-max', dest='lon_max', metavar='lon_max', type=float,
|
|---|
| 58 | help='Maximum longitude generated', default=longitude_max)
|
|---|
| 59 | parser.add_argument('-l', '--logperiod', dest='logperiod', metavar='logperiod', type=int,
|
|---|
| 60 | help='Period of data generation (seconds between two values)', default=logperiod)
|
|---|
| 61 | arguments = parser.parse_args(argv)
|
|---|
| 62 |
|
|---|
| 63 | if arguments.rrd_database is not None:
|
|---|
| 64 | rrdfile = arguments.rrd_database
|
|---|
| 65 |
|
|---|
| 66 | if arguments.lat_min is not None:
|
|---|
| 67 | latitude_min = arguments.lat_min
|
|---|
| 68 | if arguments.lat_max is not None:
|
|---|
| 69 | latitude_max = arguments.lat_max
|
|---|
| 70 |
|
|---|
| 71 | if arguments.lon_min is not None:
|
|---|
| 72 | longitude_min = arguments.lon_min
|
|---|
| 73 | if arguments.lon_max is not None:
|
|---|
| 74 | longitude_max = arguments.lon_max
|
|---|
| 75 |
|
|---|
| 76 | if arguments.logperiod is not None:
|
|---|
| 77 | logperiod = arguments.logperiod
|
|---|
| 78 |
|
|---|
| 79 | if not os.path.exists(os.path.dirname(rrdfile)):
|
|---|
| 80 | print('rrd database directory not found {}'.format(os.path.dirname(rrdfile)))
|
|---|
| 81 | parser.print_help()
|
|---|
| 82 | sys.exit(2)
|
|---|
| 83 |
|
|---|
| 84 | timestamp = int(time.time())
|
|---|
| 85 | start_time = timestamp - duration - logperiod
|
|---|
| 86 |
|
|---|
| 87 | if os.path.isfile(rrdfile):
|
|---|
| 88 | print('Removing old database {}'.format(rrdfile))
|
|---|
| 89 | os.remove(rrdfile)
|
|---|
| 90 |
|
|---|
| 91 | print('Creating {}'.format(rrdfile))
|
|---|
| 92 | daemon.rrd_create_database(rrdfile, logperiod, str(start_time))
|
|---|
| 93 |
|
|---|
| 94 | print('Filling database inside [{}, {}] --> [{}, {}]...'.format(latitude_min, longitude_min, latitude_max, longitude_max))
|
|---|
| 95 | for current_time in xrange(timestamp - duration, timestamp, logperiod):
|
|---|
| 96 | latitude = random.uniform(latitude_min, latitude_max)
|
|---|
| 97 | longitude = random.uniform(longitude_min, longitude_max)
|
|---|
| 98 | speedThroughWater = random.randint(0, 10)
|
|---|
| 99 | speedOverGround = random.randint(-2, 15)
|
|---|
| 100 | courseOverGround = random.randint(0, 360)
|
|---|
| 101 | waterDepth = random.randint(0, 20)
|
|---|
| 102 | waterTemperature = random.randint(2, 25)
|
|---|
| 103 | gpsFix = 3
|
|---|
| 104 | # update rrd database for the webservice
|
|---|
| 105 | daemon.rrd_update(rrdfile, '{}:{}:{}:{}:{}:{}:{}:{}:{}'.format(
|
|---|
| 106 | current_time, latitude, longitude, speedThroughWater, speedOverGround, courseOverGround, waterDepth, waterTemperature, gpsFix))
|
|---|
| 107 |
|
|---|
| 108 | if __name__ == "__main__":
|
|---|
| 109 | main(sys.argv[1:])
|
|---|