• Publicity: Public Only All

nsbeat-procs.tcl

This file defines the following Objects and Classes: ::nsbeat::Beat[i], ::nsbeat::Beat[i]

Location:
packages/xowiki/tcl/nsbeat-procs.tcl

Procedures in this file

Detailed information

Class ::nsbeat::Beat (public)

 ::nx::Class ::nsbeat::Beat[i]

Partial Call Graph (max 5 caller/called nodes):
%3

Testcases:
No testcase defined.

nsbeat::Beat method beat (public)

 <instance of nsbeat::Beat[i]> beat

Partial Call Graph (max 5 caller/called nodes):
%3

Testcases:
No testcase defined.

nsbeat::Beat method data (public)

 <instance of nsbeat::Beat[i]> data

Partial Call Graph (max 5 caller/called nodes):
%3

Testcases:
No testcase defined.
[ hide source ] | [ make this the default ]

Content File Source

namespace eval ::nsbeat {

    nx::Class create Beat {
        :property {outputLocation http://localhost:9200/}

        :method ms_to_utc {ms} {
            set seconds [expr {$ms / 1000}]
            set fraction [format %03d [expr {$ms - ($seconds * 1000)}]]
            return [clock format $seconds -format "%Y-%m-%dT%H:%M:%S" -gmt 1].${fraction}Z
        }

        :method nspost {data} {
            set queryHeaders [ns_set create]
            ns_set update $queryHeaders Host localhost
            ns_set update $queryHeaders Content-Type "application/json; charset=UTF-8"
            set d [ns_http run \
                       -headers $queryHeaders \
                       -method POST \
                       -body $data \
                       -timeout 10.0 \
                       ${:outputLocation}/_bulk]
            ns_http wait -result R -headers $replyHeaders -status S $h
            ns_log notice "nsbeat post returned S [dict get $d status] R [dict get $d body]"
        }

        :public method data {} {
            set index nsbeat-[clock format [clock seconds] -format "%Y.%m.%d"]
            set data ""
            foreach serverName [ns_info servers] {
                foreach poolName [lsort [ns_server -server $serverName pools]] {
                    #
                    # provide a nicer name for the pool
                    #
                    set startClicks [clock clicks -microseconds]
                    set poolLabel "default"
                    if {$poolName ne {}} {set poolLabel $poolName}
                    set rtt [expr {[clock clicks -microseconds] - $startClicks}]

                    set stats [ns_server -server $serverName -pool $poolName stats]
                    set header [subst {{"index":{"_index":"$index","_type":"doc"}}}]
                    regsub -all \n [subst $::nsbeat::template(serverStats)"" payload
                    regsub -all {\s\s+} $payload " " payload
                    append data $header\n$payload\n
                }
            }
            foreach {dbPool stats} [ns_db stats] {
                set header [subst {{"index":{"_index":"$index","_type":"doc"}}}]
                regsub -all \n [subst $::nsbeat::template(dbStats)"" payload
                regsub -all {\s\s+} $payload " " payload
                append data $header\n$payload\n
            }
            return $data
        }
        
        :public method beat {} {
            set data [:data]
            ns_log notice "nsbeat DATA:\n$data"
            :nspost $data
        }
    }

    set ::nsbeat::template(serverStats) {
        {"@timestamp":"[:ms_to_utc [clock click -milliseconds]]",
            "beat":     {"hostname":"openacs.wu-wien.ac.at","name":"openacs.wu-wien.ac.at","version":"5.6.3"},
            "metricset":{"module":"ns","name":"server","rtt":$rtt},
            "ns":   {
                "server":{
                    "name""$serverName",
                    "pool""$poolLabel",
                    "requests":    [dict get $stats requests],
                    "spools":      [dict get $stats spools],
                    "queued":      [dict get $stats queued],
                    "connthreads": [dict get $stats connthreads],
                    "accepttime":  [dict get $stats accepttime],
                    "queuetime":   [dict get $stats queuetime],
                    "filtertime":  [dict get $stats filtertime],
                    "runtime":     [dict get $stats runtime],
                    "tracetime":   [dict get $stats tracetime]
                }},"type":"metricsets"}
    }
    set ::nsbeat::template(dbStats) {
        {"@timestamp":"[:ms_to_utc [clock click -milliseconds]]",
            "beat":     {"hostname":"openacs.wu-wien.ac.at","name":"openacs.wu-wien.ac.at","version":"5.6.3"},
            "metricset":{"module":"ns","name":"db","rtt":$rtt},
            "ns":   {
                "db":{
                    "pool""$dbPool",
                    "statements":    [dict get $stats statements],
                    "gethandles":      [dict get $stats gethandles],
                    "handles":      [dict get $stats handles],
                    "used": [dict get $stats used],
                    "waittime":  [dict get $stats waittime],
                    "sqltime":   [dict get $stats sqltime]
                }},"type":"metricsets"}
    }
    Beat create b1 -outputLocation http://137.208.116.31:9200
}

ns_atstartup {
#    ns_schedule_proc 60 { ::nsbeat::b1 beat}
}