Created
August 21, 2018 16:09
-
-
Save wmohanlon/df6df38323c861b9cd111eccd52ddcbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
) | |
func fmt_event_type(s byte) string { | |
etype := make(map[byte]string) | |
// fmt.Printf("DATA %d\n", s) | |
etype[0x46] = "Log Repository Cleared" | |
etype[0x3] = "Voltage Fail" | |
etype[0x5] = "Power State Change" | |
etype[0x6] = "AC Fail" | |
return etype[s] | |
} | |
func fmt_element_type(s byte) string { | |
etype := make(map[byte]string) | |
etype[0x07] = "Log Repository Cleared" | |
etype[0x02] = "Power Supply" | |
etype[0x03] = "Cooling" | |
etype[0x04] = "Temperature" | |
etype[0x12] = "Voltage" | |
etype[0x13] = "Current" | |
return etype[s] | |
} | |
func fmt_element_name(s byte) string { | |
etype := make(map[byte]string) | |
etype[0x07] = "ESCE" | |
etype[0x02] = "Power Supply" | |
etype[0x03] = "Cooling" | |
etype[0x04] = "Temperature Sensor" | |
etype[0x12] = "Voltage Sensor" | |
etype[0x13] = "Current Sensor" | |
etype[0x18] = "SAS Expander" | |
etype[0x19] = "SAS Connector" | |
return etype[s] | |
} | |
func fmt_voltage(s byte) float32 { | |
i := int(s) | |
f := float32(i) / 100.0 | |
return f | |
} | |
func fmt_status(s byte) string { | |
etype := make(map[byte]string) | |
etype[0x01] = "OK" | |
etype[0x02] = "Critical" | |
etype[0x03] = "Noncritical" | |
etype[0x05] = "Not installed" | |
etype[0x06] = "Unknown" | |
etype[0x07] = "Not Available" | |
return etype[s] | |
} | |
func fmt_attr(s byte) string { | |
var res string | |
//fmt.Printf("X %d\n", (s&0xC0)>>6) | |
if ((s & 0xC0) >> 6) == 0 { | |
// res += "Insert/Assert/On " | |
res += "On " | |
} | |
if ((s & 0xC0) >> 6) == 3 { | |
// res += "Remove/Deassert/Off " | |
res += "Off " | |
} | |
if ((s & 0x30) >> 4) == 00 { | |
// res += "Not Applicable " | |
res += "" | |
} | |
//fmt.Printf("X %d\n", (s&0x30)>>4) | |
if ((s & 0x30) >> 4) == 1 { | |
res += "Over High Thresh " | |
} | |
if ((s & 0x30) >> 4) == 2 { | |
res += "Under Low Thresh " | |
} | |
/* bits 2 & 3 are Cleared Event Severity */ | |
if (s & 0x03) == 0 { | |
res += "Info" | |
} | |
if (s & 0x03) == 1 { | |
res += "Warning" | |
} | |
if (s & 0x03) == 2 { | |
res += "Critical" | |
} | |
return res | |
} | |
func get_bytes() { | |
b, err := ioutil.ReadFile("dump.txt") // just pass the file name | |
if err != nil { | |
panic("failed file read") | |
} | |
//fmt.Println(b) // print the content as 'bytes' | |
// str := string(b) // convert content to a 'string' | |
// fmt.Println(str) // print the content as a | |
parse(b) | |
} | |
func parse(inbytes []byte) { | |
if len(inbytes) < 16 { | |
fmt.Print("No parseable data ") | |
fmt.Println(inbytes) | |
os.Exit(0) | |
} | |
p := inbytes[12:] | |
for len(p) >= 16 { | |
log_entry := p[:16] | |
p = p[16:] | |
// fmt.Println(log_entry) | |
parse_entry(log_entry) | |
} | |
} | |
func parse_entry(logent []byte) { | |
if len(logent) < 16 { | |
panic("short entry") | |
} | |
log_entry_id := logent[0]<<8 + logent[1] | |
// log_entry_version := logent[2] | |
log_type := logent[3] | |
log_timestamp := (int(logent[4]) << 24) + | |
(int(logent[5]) << 16) + | |
(int(logent[6]) << 8) + | |
(int(logent[7])) | |
log_element_type := logent[9] | |
log_element_id := logent[10] | |
log_event_type := logent[11] | |
log_event_attributes := logent[12] | |
log_event_data := logent[13:] | |
fmt.Printf("ID: %-4d Type: %-4d Timestamp: %-20s %-20s %-4d %-20s %-40s Data ", | |
log_entry_id, log_type, fmt_date(log_timestamp), fmt_element_name(log_element_type), log_element_id, | |
fmt_event_type(log_event_type), fmt_attr(log_event_attributes)) | |
//fmt.Print(log_event_data) | |
fmt.Printf("%2.3f\n", fmt_voltage(log_event_data[0])) | |
} | |
func fmt_date(d int) string { | |
day := d / 86400 | |
d = d % 86400 | |
hour := d / 3600 | |
d = d % 3600 | |
min := d / 60 | |
d = d % 60 | |
sec := d | |
result := fmt.Sprintf("Day: %d Time: %02d:%02d:%02d", day, hour, min, sec) | |
return (result) | |
} | |
func main() { | |
args := os.Args | |
enc := "/dev/ses0" | |
clear := false | |
side := "00" | |
for i := 0; i < len(args); i++ { | |
if args[i] == "-e" { | |
i++ | |
enc = args[i] | |
break | |
} | |
if args[i] == "-c" { | |
clear = true | |
} | |
if args[i] == "-s" { | |
i++ | |
side = args[i] | |
if side != "00" && side != "01" { | |
fmt.Println("side must be 00 or 01") | |
os.Exit(1) | |
} | |
} | |
} | |
dev := enc | |
if _, err := os.Stat(dev); os.IsNotExist(err) { | |
fmt.Println("Device does not exist: ", dev) | |
os.Exit(1) | |
} | |
/* Dump logs if we're not clearing */ | |
if !clear { | |
/* sg_senddiag -p -r 13,00,00,06,02,00,00,01,ff,ff /dev/ses0 */ | |
scmd := "13,00,00,06,02," + side + ",00,01,ff,ff" | |
fmt.Println("cmd: sg_senddiag -p -r " + scmd + " " + dev) | |
cmd := exec.Command("sg_senddiag", "-p", "-r", scmd, dev) | |
bytes, err := cmd.CombinedOutput() | |
if err != nil { | |
panic("failed cmd exec " + string(bytes)) | |
} | |
cmd = exec.Command("sg_ses", "-rr", "-p", "0x13", dev) | |
bytes, err = cmd.CombinedOutput() | |
if err != nil { | |
panic("failed cmd exec " + string(bytes)) | |
} | |
parse(bytes) | |
} | |
if clear { | |
fmt.Println("Clearing logs\n") | |
scmd := "13,00,00,03,01," + side + ",03" | |
fmt.Println("cmd: sg_senddiag -p -r " + scmd + " " + dev) | |
cmd := exec.Command("sg_senddiag", "-p", "-r", scmd, dev) | |
bytes, err := cmd.CombinedOutput() | |
fmt.Println("First Result: " + string(bytes)) | |
cmd = exec.Command("sg_ses", "-p", "0x13", dev) | |
bytes, err = cmd.CombinedOutput() | |
if err != nil { | |
panic("failed cmd exec") | |
} | |
fmt.Println("Result: " + string(bytes)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment