netdata_tsrelay.nim
author Mahlon E. Smith <mahlon@laika.com>
Wed, 14 Feb 2018 17:27:29 -0800
changeset 6 1f366fc61592
parent 5 a1276c3d39eb
child 7 c0bcf3bea772
permissions -rw-r--r--
Each incoming connection requires its own client socket.
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,
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    34
    math,
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    35
    nativesockets,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    36
    net,
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    37
    os,
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    38
    parseopt2,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    39
    strutils,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    40
    tables,
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    41
    terminal,
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    42
    times,
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    43
    threadpool
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    44
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    45
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    46
const
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    47
    VERSION = "v0.1.0"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    48
    USAGE = """
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    49
./netdata_tsrelay [-q][-v][-h] --dbopts="[PostgreSQL connection string]" --listen-port=14866 --listen-addr=0.0.0.0
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    50
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    51
  -q: Quiet mode.  No output at all.  Ignored if -d is supplied.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    52
  -c: Suppress ANSI color output.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    53
  -d: Debug: Show incoming and parsed data.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    54
  -v: Display version number.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    55
  -h: Help.  You're lookin' at it.
0
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
The default connection string is:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    58
  "host=localhost port=5432 dbname=netdata user=netdata application_name=netdata-tsrelay"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    59
    """
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    60
    INSERT_SQL = """
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    61
    INSERT INTO netdata
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    62
        ( time, host, metrics )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    63
    VALUES
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    64
        ( 'epoch'::timestamptz + ? * '1 second'::interval, ?, ? )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    65
    """
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    66
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    67
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    68
type
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    69
    Config = object of RootObj
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    70
        dbopts:      string  # The postgresql connection parameters.  (See https://www.postgresql.org/docs/current/static/libpq-connect.html)
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    71
        listen_port: int     # The port to listen for incoming connections
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    72
        listen_addr: string  # The IP address listen for incoming connections.  Defaults to inaddr_any.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    73
        verbose:     bool    # Be informative
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    74
        debug:       bool    # Spew out raw data
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    75
        use_color:   bool    # Pretty things up a little, probably want to disable this if debugging
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    76
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    77
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    78
# The global config object
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    79
#
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    80
# FIXME:  Rather than pass this all over the
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    81
# place, consider channels and createThread instead of spawn.
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    82
#
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    83
var conf = Config(
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    84
    dbopts: "host=localhost port=5432 dbname=netdata user=netdata application_name=netdata-tsrelay",
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    85
    listen_port: 14866,
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    86
    listen_addr: "0.0.0.0",
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    87
    verbose: true,
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    88
    debug: false,
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    89
    use_color: true
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    90
)
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    91
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    92
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    93
proc hl( msg: string, fg: ForegroundColor, bright=false ): string =
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    94
    ## Quick wrapper for color formatting a string, since the 'terminal'
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    95
    ## module only deals with stdout directly.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    96
    if not conf.use_color: return msg
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    97
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    98
    var color: BiggestInt = ord( fg )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
    99
    if bright: inc( color, 60 )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   100
    result = "\e[" & $color & 'm' & msg & "\e[0m"
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   101
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   102
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   103
proc fetch_data( client: Socket ): string =
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   104
    ## 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
   105
    ## line and wait for stream timeout to determine a "sample".
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   106
    try:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   107
        result = client.recv_line( timeout=500 ) & "\n"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   108
        while result != "":
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   109
            result = result & client.recv_line( timeout=500 ) & "\n"
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   110
    except TimeoutError:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   111
        discard
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   112
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   113
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   114
proc parse_data( data: string, conf: Config ): Table[ BiggestInt, JsonNode ] =
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   115
    ## Given a raw +data+ string, parse JSON and return a table of
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   116
    ## JSON samples ready for writing, keyed by timestamp. Netdata can
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   117
    ## buffer multiple samples in one batch.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   118
    if data == "": return
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
    # Hash of sample timeperiods to pivoted json data
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   121
    result = init_table[ BiggestInt, JsonNode ]()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   122
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   123
    for sample in split_lines( data ):
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   124
        if conf.debug: echo sample.hl( fgBlack, bright=true )
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   125
        if sample.len == 0: continue
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   126
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   127
        var parsed: JsonNode
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   128
        try:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   129
            parsed = sample.parse_json
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   130
        except JsonParsingError:
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   131
            discard
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   132
            if conf.debug: echo hl( "Unable to parse sample line: " & sample.hl(fgRed, bright=true), fgRed )
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   133
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   134
        # Create or use existing Json object for modded data.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   135
        #
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   136
        var pivot: JsonNode
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   137
        let key = parsed["timestamp"].get_num
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   138
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   139
        if result.has_key( key ):
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   140
            pivot = result[ key ]
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   141
        else:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   142
            pivot = newJObject()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   143
            result[ key ] = pivot
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   144
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   145
        var name = parsed[ "chart_id" ].get_str & "." & parsed[ "id" ].get_str
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   146
        pivot[ "hostname" ] = parsed[ "hostname" ]
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   147
        pivot[ name ] = parsed[ "value" ]
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
    return result
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   150
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   151
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   152
proc process( client: Socket, db: DBConn, conf: Config ): int =
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   153
    ## Do the work for a connected client within a thread.
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   154
    ## Returns the number of samples parsed.
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   155
    var raw_data = client.fetch_data
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   156
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   157
    # Done with the socket, netdata will automatically
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   158
    # reconnect.  Save local resources/file descriptors
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   159
    # by closing after the send is considered complete.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   160
    #
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   161
    try:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   162
        client.close
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   163
    except OSError:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   164
        return
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   165
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   166
    # Pivot data and save to SQL.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   167
    #
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   168
    var samples = parse_data( raw_data, conf )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   169
    if samples.len != 0:
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   170
        db.exec sql( "BEGIN" )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   171
        for timestamp, sample in samples:
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   172
            var host = sample[ "hostname" ].get_str
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   173
            sample.delete( "hostname" )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   174
            db.exec sql( INSERT_SQL ), timestamp, host, sample
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   175
        db.exec sql( "COMMIT" )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   176
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   177
    return samples.len
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   178
4
f3d83bdd7877 Wrap commits in a transaction.
Mahlon E. Smith <mahlon@laika.com>
parents: 1
diff changeset
   179
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   180
proc runthread( client: Socket, address: string, db: DBConn, conf: Config ): void {.thread.} =
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   181
    ## A thread that performs that dispatches processing and returns
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   182
    ## results.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   183
    let t0 = cpu_time()
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   184
    var samples = client.process( db, conf )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   185
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   186
    if conf.verbose:
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   187
        echo(
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   188
            hl( $samples, fgWhite, bright=true ),
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   189
            " sample(s) parsed from ",
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   190
            address.hl( fgYellow, bright=true ),
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   191
            " in ", hl($( round(cpu_time() - t0, 3) ), fgWhite, bright=true), " seconds."
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   192
            # " ", hl($(round((get_occupied_mem()/1024/1024),1)), fgWhite, bright=true), "MB memory used."
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   193
        )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   194
    when defined( testing ): dumpNumberOfInstances()
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   195
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   196
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   197
proc serverloop: void =
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   198
    ## Open a database connection, bind to the listening socket,
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   199
    ## and start serving incoming netdata streams.
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   200
    let db = open( "", "", "", conf.dbopts )
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   201
    if conf.verbose: echo( "Successfully connected to the backend database.".hl( fgGreen ) )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   202
6
1f366fc61592 Each incoming connection requires its own client socket.
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
   203
    var server = newSocket()
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   204
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   205
    server.set_sock_opt( OptReuseAddr, true )
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   206
    server.bind_addr( Port(conf.listen_port), conf.listen_addr )
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   207
    server.listen()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   208
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   209
    if conf.verbose:
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   210
        echo(
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   211
            "Listening for incoming connections on ".hl( fgGreen, bright=true ),
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   212
            hl( (if conf.listen_addr == "0.0.0.0": "*" else: conf.listen_addr) , fgBlue, bright=true ),
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   213
            ":",
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   214
            hl( $conf.listen_port, fgBlue, bright=true ),
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   215
        )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   216
        echo ""
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   217
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   218
    while true:
6
1f366fc61592 Each incoming connection requires its own client socket.
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
   219
        var client  = newSocket()
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   220
        var address = ""
6
1f366fc61592 Each incoming connection requires its own client socket.
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
   221
1f366fc61592 Each incoming connection requires its own client socket.
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
   222
        client.close
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   223
        server.acceptAddr( client, address ) # blocking call
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   224
        spawn runthread( client, address, db, conf )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   225
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   226
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   227
proc atexit() {.noconv.} =
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   228
    ## Exit cleanly after waiting on any running threads.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   229
    echo "Exiting..."
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   230
    sync()
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   231
    quit( 0 )
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   232
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   233
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   234
proc parse_cmdline: void =
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   235
    ## Populate the config object with the user's preferences.
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   236
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   237
    # always set debug mode if development build.
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   238
    conf.debug = defined( testing )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   239
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   240
    for kind, key, val in getopt():
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   241
        case kind
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   242
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   243
        of cmdArgument:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   244
            discard
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   245
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   246
        of cmdLongOption, cmdShortOption:
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   247
            case key
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   248
                of "debug", "d":
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   249
                    conf.debug = true
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   250
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   251
                of "no-color", "c":
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   252
                    conf.use_color = false
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   253
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   254
                of "help", "h":
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   255
                    echo USAGE
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   256
                    quit( 0 )
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   257
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   258
                of "quiet", "q":
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   259
                    conf.verbose = false
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   260
            
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   261
                of "version", "v":
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   262
                    echo hl( "netdata_tsrelay " & VERSION, fgWhite, bright=true )
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   263
                    quit( 0 )
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   264
               
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   265
                of "dbopts": conf.dbopts = val
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   266
                of "listen-addr", "a": conf.listen_addr = val
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   267
                of "listen-port", "p": conf.listen_port = val.parse_int
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   268
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   269
                else: discard
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   270
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   271
        of cmdEnd: assert( false ) # shouldn't reach here ever
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   272
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   273
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   274
when isMainModule:
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   275
    system.addQuitProc( resetAttributes )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   276
    system.addQuitProc( atexit )
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   277
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   278
    parse_cmdline()
5
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   279
a1276c3d39eb Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
   280
    if conf.debug: echo hl( $conf, fgYellow )
0
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   281
    serverloop()
72c9c6f0b713 Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   282