netdata_tsrelay.nim
author Mahlon E. Smith <mahlon@laika.com>
Mon, 12 Feb 2018 13:00:25 -0800
changeset 3 f34855556f4d
parent 1 160338bb2822
child 4 f3d83bdd7877
permissions -rw-r--r--
Add timescale creation example.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     1
# vim: set et nosta sw=4 ts=4 ft=nim : 
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     2
#
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     3
# Copyright (c) 2018, Mahlon E. Smith <mahlon@martini.nu>
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     4
# All rights reserved.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     5
# Redistribution and use in source and binary forms, with or without
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     6
# modification, are permitted provided that the following conditions are met:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     7
#
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     8
#     * Redistributions of source code must retain the above copyright
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     9
#       notice, this list of conditions and the following disclaimer.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    10
#
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    11
#     * Redistributions in binary form must reproduce the above copyright
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    12
#       notice, this list of conditions and the following disclaimer in the
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    13
#       documentation and/or other materials provided with the distribution.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    14
#
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    15
#     * Neither the name of Mahlon E. Smith nor the names of his
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    16
#       contributors may be used to endorse or promote products derived
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    17
#       from this software without specific prior written permission.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    18
#
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    19
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    20
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    21
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    22
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    23
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    24
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    25
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    26
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    27
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    28
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    29
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    30
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    31
import
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    32
    db_postgres,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    33
    json,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    34
    nativesockets,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    35
    net,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    36
    parseopt2,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    37
    strutils,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    38
    tables,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    39
    threadpool
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    40
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    41
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    42
const
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    43
    VERSION = "v0.1.0"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    44
    USAGE = """
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    45
./netdata_tsrelay --dbopts="[PostgreSQL connection string]" --listen-port=14866
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    46
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    47
The default connection string is:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    48
  "host=localhost port=5432 dbname=netdata user=netdata application_name=netdata-tsrelay"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    49
    """
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    50
    INSERT_SQL = """
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    51
    INSERT INTO netdata
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    52
        ( time, host, metrics )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    53
    VALUES
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    54
        ( 'epoch'::timestamptz + ? * '1 second'::interval, ?, ? )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    55
    """
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    56
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    57
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    58
type Config = object of RootObj
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    59
    dbopts:      string  # The postgresql connection parameters.  (See https://www.postgresql.org/docs/current/static/libpq-connect.html)
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    60
    listen_port: int     # The port to listen for incoming connections
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    61
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    62
# Global config object
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    63
#
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    64
var conf = Config(
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    65
    dbopts: "host=localhost port=5432 dbname=netdata user=netdata application_name=netdata-tsrelay",
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    66
    listen_port: 14866
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    67
)
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    68
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    69
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    70
proc fetch_data( client: Socket ): string =
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    71
    ## Netdata JSON backend doesn't send a length, so we read line by
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    72
    ## line and wait for stream timeout to determine a "sample".
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    73
    try:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    74
        result = client.recv_line( timeout=500 ) & "\n"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    75
        while result != "":
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    76
            result = result & client.recv_line( timeout=500 ) & "\n"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    77
    except TimeoutError:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    78
        discard
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    79
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    80
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    81
proc parse_data( data: string ): Table[ BiggestInt, JsonNode ] =
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    82
    ## Given a raw +data+ string, parse JSON and return a table of
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    83
    ## JSON samples ready for writing, keyed by timestamp. Netdata can
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    84
    ## buffer multiple samples in one batch.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    85
    if data == "": return
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    86
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    87
    # Hash of sample timeperiods to pivoted json data
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    88
    result = init_table[ BiggestInt, JsonNode ]()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    89
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    90
    for sample in split_lines( data ):
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    91
        if defined( testing ): echo sample
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    92
        if sample.len == 0: continue
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    93
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    94
        var parsed: JsonNode
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    95
        try:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    96
            parsed = sample.parse_json
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    97
        except JsonParsingError:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    98
            if defined( testing ): echo "Unable to parse sample line: " & sample
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    99
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   100
        # Create or use existing Json object for modded data.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   101
        #
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   102
        var pivot: JsonNode
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   103
        let key = parsed["timestamp"].get_num
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   104
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   105
        if result.has_key( key ):
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   106
            pivot = result[ key ]
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   107
        else:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   108
            pivot = newJObject()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   109
            result[ key ] = pivot
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   110
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   111
        var name = parsed[ "chart_id" ].get_str & "." & parsed[ "id" ].get_str
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   112
        pivot[ "hostname" ] = parsed[ "hostname" ]
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   113
        pivot[ name ] = parsed[ "value" ]
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   114
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   115
    if defined( testing ): echo $result.len & " samples"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   116
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   117
    return result
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   118
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   119
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   120
proc process( client: Socket, db: DBConn ): void =
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   121
    ## Do the work for a connected client within a thread.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   122
    var raw_data = client.fetch_data
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   123
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   124
    try:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   125
        if defined( testing ):
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   126
            echo "Closed connection for " & get_peer_addr( client.get_fd, get_sock_domain(client.get_fd) )[0]
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   127
        client.close
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   128
    except OSError:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   129
        return
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   130
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   131
    var samples = parse_data( raw_data )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   132
    for timestamp, sample in samples:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   133
        var host = sample[ "hostname" ].get_str
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   134
        sample.delete( "hostname" )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   135
        db.exec sql( INSERT_SQL ), timestamp, host, sample
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   136
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   137
proc serverloop: void =
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   138
    ## Open a database connection, bind to the listening socket,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   139
    ## and start serving incoming netdata streams.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   140
    let db = open( "", "", "", conf.dbopts )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   141
    echo "Successfully connected to the backend database."
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   142
    
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   143
    var server = newSocket()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   144
    echo "Listening for incoming connections on port ", conf.listen_port, "..."
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   145
    server.set_sock_opt( OptReuseAddr, true )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   146
    server.bind_addr( Port(conf.listen_port) )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   147
    server.listen()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   148
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   149
    while true:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   150
        var
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   151
            client  = newSocket()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   152
            address = ""
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   153
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   154
        server.acceptAddr( client, address )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   155
        echo "New connection: " & address
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   156
        spawn client.process( db )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   157
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   158
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   159
proc parse_cmdline: void =
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   160
    ## Populate the config object with the user's preferences.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   161
    for kind, key, val in getopt():
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   162
        case kind
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   163
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   164
        of cmdArgument:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   165
            discard
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   166
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   167
        of cmdLongOption, cmdShortOption:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   168
            case key
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   169
                of "help", "h":
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   170
                    echo USAGE
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   171
                    quit( 0 )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   172
            
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   173
                of "version", "v":
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   174
                    echo "netdata_tsrelay ", VERSION
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   175
                    quit( 0 )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   176
               
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   177
                of "dbopts": conf.dbopts = val
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   178
                of "listen-port", "p": conf.listen_port = val.parse_int
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   179
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   180
                else: discard
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   181
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   182
        of cmdEnd: assert( false ) # shouldn't reach here ever
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   183
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   184
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   185
when isMainModule:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   186
    parse_cmdline()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   187
    if defined( testing ): echo conf
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   188
    serverloop()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   189