4 ms·
TXR: @(bind idtab @(relate '("radeon" "k10temp") '("GPU" "CPU") "SYS")) @(bind thash @(hash)) @(repeat
by kazinator 5y ago
TXR:
@(bind idtab @(relate '("radeon" "k10temp")
'("GPU" "CPU")
"SYS"))
@(bind thash @(hash))
@(repeat)
@id-@bus-@code
Adapter: @name
@ (repeat)
temp@num: @{temp}°C@nil
@ (do (set [thash `@[idtab id]_@num`] temp))
@ (until)
@ (end)
@(end)
@(do (dohash (tag temp thash)
(sh `echo gmetric -t uint16 -u Celsius -n @tag -v @temp`)))
$ txr temp.txr temp.dat
gmetric -t uint16 -u Celsius -n SYS_2 -v +43.0
gmetric -t uint16 -u Celsius -n SYS_1 -v +31.0
gmetric -t uint16 -u Celsius -n CPU_1 -v +36.8
gmetric -t uint16 -u Celsius -n GPU_1 -v +50.5
gmetric -t uint16 -u Celsius -n SYS_3 -v +38.0
That's based on what I think the Awk is stuffing into the associative array. I was not able to run the code as pasted verbatim from the site:
$ gawk -f temp.awk temp.dat
gawk: temp.awk:32: temp = substr(
gawk: temp.awk:32: ^ unexpected newline or end of string
gawk: temp.awk:35: matches[1, "length"]
gawk: temp.awk:35: ^ unexpected newline or end of string
gawk: temp.awk:35: );
gawk: temp.awk:35: ^ 0 is invalid as number of arguments for substr
$ mawk -f temp.awk temp.dat
mawk: temp.awk: line 30: regular expression compile failed (missing operand)
+([0-9.]+)°C
mawk: temp.awk: line 30: syntax error at or near ,
mawk: temp.awk: line 31: missing ) near end of line
mawk: temp.awk: line 32: syntax error at or near ,
mawk: temp.awk: line 35: extra ')'
I suspect the author may have tweaked the code for presentation in the blog without rechecking that it still works. Or else it needs some specific implementation and version of awk, with specific command line arguments that are not given, unfortunately.
- kazinator 5y agoHere is the data into JSON (keeping the values a $ txr json.txr temp.dat {"radeon-pci-0100":{"temp1":{"crit":120,"hyst":90,"value":50.5}}, "f71889ed-isa-0480":{"temp2":{"hyst":77,"value":43,"high":85},"alarm":{"fan3":true,"fan2":true}, "sensor":{"crit":100,"name":"thermistor","hyst":92},"max-voltage":{"in1":2.04}, "temp1":{"hyst":81,"value":31,"high":85},"voltage":{"in5":1.23,"+3.3V":3.23,"in6":1.53,"in2":1.09,"Vbat":3.31,"in1":1.07, "in4":0.58,"3VSB":3.25,"in3":0.89}, "temp3":{"hyst":68,"value":38,"high":70},"rpm":{"fan3":0,"fan1":3978,"fan2":0}}, "k10temp-pci-00c3":{"temp1":{"crit":80,"hyst":78,"value":36.8,"high":70}}} Using just a straightforward approach of recognizing the cases that occur without trying to formally parse things. There is significant copy and paste between similar cases. I decided to use a post-processing pass on the dictionary to convert the numeric values to floating-point. @(bind dict @(hash)) @(name file) @(repeat) @idstring Adapter: @adapter @ (collect :vars (entry)) @ (line line) @ (assert error `unhandled stuff occurs at @file:@line`) @ (some) @{temp /temp\d+/}: @{val}°C (crit = @{crit}°C, hyst = @{hyst}°C) @ (bind entry @#J^{~temp : { "value" : ~val, "crit" : ~crit, "hyst" : ~hyst }}) @ (or) @{temp /temp\d+/}: @{val}°C (high = @{high}°C) (crit = @{crit}°C, hyst = @{hyst}°C) @ (bind entry @#J^{~temp : { "value" : ~val, "crit" : ~crit, "hyst" : ~hyst, "high" : ~high }}) @ (or) @{temp /temp\d+/}: @{val}°C (high = @{high}°C, hyst = @{hyst}°C) @ (bind entry @#J^{~temp : { "value" : ~val, "high" : ~high, "hyst" : ~hyst }}) @ (or) (crit = @{crit}°C, hyst = @{hyst}°C) sensor = @sensor @ (bind entry @#J^{"sensor" : { "name" : ~sensor, "crit" : ~crit, "hyst" : ~hyst }}) @ (or) @label: @voltage V @ (bind entry @#J^{"voltage" : {~label : ~voltage}}) @ (or) @label: @voltage V (max = @max V) @ (bind entry @#J^{"voltage" : {~label : ~voltage}, "max-voltage" : {~label : ~max}}) @ (or) @label: @rpm RPM @ (bind entry @#J^{"rpm" : {~label : ~rpm}}) @ (or) @label: @rpm RPM ALARM @ (bind entry @#J^{"rpm" : {~label : ~rpm}, "alarm" : {~label : true}}) @ (or) @ (bind entry @#J{}) @ (end) @ (until) @ (end) @ (do (set [dict idstring] (reduce-left (op hash-uni @1 @2 hash-uni) entry #J{}))) @(end) @(do (defun numify (dict) (dohash (k v dict dict) (typecase v (string (iflet ((f (tofloat v))) (set [dict k] f))) (hash (numify v))))) (put-jsonl (numify dict)))
- vcdimension 5y agoMy first thought when I saw this item in the HN list was TXR :)
- kazinator 5y agoThe blog's code works (with gawk) if some whitespace errors are fixed. $ gawk -f temp.awk temp.dat # echo inserted into command gmetric -t uint16 -u Celsius -n GPU_1 -v 50.5 gmetric -t uint16 -u Celsius -n SYS_1 -v 31.0 gmetric -t uint16 -u Celsius -n SYS_2 -v 43.0 gmetric -t uint16 -u Celsius -n SYS_3 -v 38.0 gmetric -t uint16 -u Celsius -n CPU_1 -v 36.8 I think they are a design flaw in Awk; I'm going to look into that and recommend changes to POSIX via the Austin Group mailing list if it still exists. Awk has some newline sensitivities due to the following ambiguities: condition # condition with no action allowed: default { print } action { action } # action with no condition allowed condition { action } # both Therefore, this is not allowed (or well, it is, but codifies a separate condition with a default action, and an unconditional action). condition { action } There can be no newline between a condition and the opening { of its action. And actions must be brace enclosed. And thus (IIRC) the awk lexical analyzer (in the original One True Awk implementation) returns an explicit newline token to the Yacc parser. In any phrase structure that doesn't deal with that token, a newline will cause a syntax error: function( # no good arg ) function("string " # no good foo + bar " catenation") When the lexer produces the token which is the opening brace of an action, it could shift into a freeform state, in which it consumes newlines internally. Then when the action is parsed, it can be returned to the newline-sensitive mode. The newline sensitivities don't seem to serve a purpose in the C-like language within the actions. That language also occurs outside of actions via the function construct: function whatever(...) { } here the lexer would also be shifted into the freeform mode, as appropriate.