· 6 years ago · Aug 28, 2019, 07:16 PM
1# Storj3Monitor script by Krey
2# this script gathers, aggregate displays and monitor all you node threasholds
3# if uptime or audit down by 2 script send email to you
4
5# Changes:
6# v0.0 - 20190828 Initial version, only displays data
7
8$v = "0.0"
9
10#!!!!!!!!!!!!! Fill this with you dashboard api addresses for each node. Default is one 127.0.0.1:14002
11$config = @{
12 Nodes =
13 "127.0.0.1:14002"
14 #"localhost:4401",
15 #"localhost:4402",
16 #"localhost:4403",
17 #"localhost:4404"
18}
19
20function Preamble{
21 Write-Host ("Storj3Monitor script by Krey ver {0}" -f $v)
22 Write-Host -ForegroundColor Yellow "I work on beer. If you like my scripts please donate bottle of beer in STORJ or ETH to 7df3157909face2dd972019d590adba65d83b1d8"
23 Write-Host -ForegroundColor Yellow "mail-to: krey@irinium.ru"
24}
25
26function GetJson
27{
28 param($uri)
29
30 $resp = Invoke-WebRequest -Uri $uri
31 if ($resp.StatusCode -ne 200) { throw $resp.StatusDescription }
32 $json = ConvertFrom-Json $resp.Content
33 if (-not [System.String]::IsNullOrEmpty($json.Error)) { throw $json.Error }
34 else { $json = $json.data }
35 return $json
36}
37
38function GetNodes
39{
40 param ($config)
41 $result = [System.Collections.Generic.List[PSCustomObject]]@()
42
43 $config.Nodes | ForEach-Object {
44 $address = $_
45 try {
46 $dash = GetJson -uri ("http://{0}/api/dashboard" -f $address)
47 $dash | Add-Member -NotePropertyName Sat -NotePropertyValue ([System.Collections.Generic.List[PSCustomObject]]@())
48
49 $dash.satellites | ForEach-Object {
50 $satid = $_
51 try {
52 $sat = GetJson -uri ("http://{0}/api/satellite/{1}" -f $address, $satid)
53 $dash.Sat.Add($sat)
54 }
55 catch {
56 Write-Host -ForegroundColor Red ("Node on address {0} fail sat {1}: {2}" -f $address, $satid, $_.Exception.Message )
57 }
58 }
59 $result.Add($dash)
60 }
61 catch {
62 Write-Host -ForegroundColor Red ("Node on address {0} fail: {1}" -f $address, $_.Exception.Message )
63 }
64 }
65 return $result
66}
67
68function GetScore
69{
70 param($nodes)
71 #$result = [System.Collections.Generic.List[PSObject]]@()
72 $nodes | ForEach-Object {
73 $node = $_
74 $node.Sat | ForEach-Object {
75 $sat = $_
76 New-Object PSObject -Property @{
77 Key = ("{0}-{1}" -f $node.nodeID, $sat.id)
78 NodeId = $node.nodeID
79 SatelliteId = $sat.id
80 Audit = $sat.audit.score
81 Uptime = $sat.uptime.score
82 Ingress = $sat.bandwidthDaily.ingress.repair + $sat.bandwidthDaily.ingress.usage
83 Egress = $sat.bandwidthDaily.egress.repair + $sat.bandwidthDaily.egress.usage
84 }
85 }
86 }
87}
88function Compact
89{
90 param($id)
91 return $id.Substring(0,4) + "-" + $id.Substring($id.Length-2)
92}
93
94function Round
95{
96 param($value)
97 return [Math]::Round($value * 100, 2)
98}
99
100function HumanBytes {
101 param ([int64]$bytes)
102 $suff = "bytes", "KiB", "MiB", "GiB", "TiB", "PiB"
103 $level = 0
104 $rest = [double]$bytes
105 while ([Math]::Abs($rest/1024) -ge 1) {
106 $level++
107 $rest = $rest/1024
108 }
109 $mant = [Math]::Max(3 - [Math]::Floor($rest).ToString().Length,0)
110 return ("{0} {1}" -f [Math]::Round($rest,$mant), $suff[$level])
111}
112
113Preamble
114$nodes = GetNodes -config $config
115$score = GetScore -nodes $nodes
116$score | Sort-Object SatelliteId, NodeId | Format-Table `
117 @{n='Satellite';e={Compact($_.SatelliteId)}}, `
118 @{n='Node';e={Compact($_.NodeId)}}, `
119 @{n='Ingress';e={HumanBytes($_.Ingress)}}, `
120 @{n='Egress';e={HumanBytes($_.Egress)}}, `
121 @{n='Audit';e={Round($_.Audit)}}, `
122 @{n='Uptime';e={Round($_.Uptime)}}