github.com/alexis81/domosgo@v0.0.0-20191016125037-5aee90a434af/DomosV2/src/statsRaspberry.go (about) 1 package main 2 3 //--------------------------------------------------------------------------------------------------------------------------------------------------------- 4 // Package : statsRaspberry.go 5 // Port rest : 1001 6 // Gets : 7 // /Stats 8 //--------------------------------------------------------------------------------------------------------------------------------------------------------- 9 10 import ( 11 "encoding/json" 12 "fmt" 13 "net/http" 14 "strconv" 15 //"reflect" 16 //fmt.Println(reflect.TypeOf(stats)) 17 "io/ioutil" 18 "os" 19 "os/exec" 20 "regexp" 21 "time" 22 23 "github.com/shirou/gopsutil/cpu" 24 "github.com/shirou/gopsutil/mem" 25 "gopkg.in/yaml.v2" 26 ) 27 28 var ( 29 version = "1.0.1" 30 lectureInit LectureInit 31 ) 32 33 // Structure du Json 34 type Stats struct { 35 Type string `json:"type"` 36 Domos_poolhouse_localTechnique_Raspberry_Stats_Cpu string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Cpu"` 37 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Total string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Total"` 38 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Available string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Available"` 39 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Used string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Used"` 40 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_UsedPourcent string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.UsedPourcent"` 41 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_UsedFree string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.UsedFree"` 42 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_UsedActive string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.UsedActive"` 43 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_UsedInactive string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.UsedInactive"` 44 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Wired string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Wired"` 45 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Buffers string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Buffers"` 46 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Cached string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Cached"` 47 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Writeback string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Writeback"` 48 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Dirty string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Dirty"` 49 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_WritebackTmp string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.WritebackTmp"` 50 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Shared string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Shared"` 51 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Slab string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.Slab"` 52 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_PageTables string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.PageTables"` 53 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_SwapCached string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Mem.SwapCached"` 54 Domos_poolhouse_localTechnique_Raspberry_Stats_Temperature string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Temperature"` 55 Domos_poolhouse_localTechnique_Raspberry_Stats_Frequence string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Frequence"` 56 Domos_poolhouse_localTechnique_Raspberry_Stats_Elapsed string `json:"Domos.poolhouse.localTechnique.Raspberry.Stats.Elapsed"` 57 Version string `json:"version"` 58 } 59 60 // Structure pour le fichier de configuration 61 type LectureInit struct { 62 Raspberry struct { 63 Url string `yaml:"url"` 64 Port string `yaml:"port"` 65 } `yaml:"Raspberry"` 66 } 67 68 // Fonction main 69 func main() { 70 71 // Lecture du fichier config.yaml 72 //filename := filepath.IsAbs("/root/DomosV2/src/Conf/config.yaml") 73 yamlFile, err := ioutil.ReadFile("/root/DomosV2/src/Conf/config.yaml") 74 if err != nil { 75 fmt.Println("Erreur de lecture du fichier config.yaml") 76 fmt.Println(err) 77 os.Exit(1) 78 } 79 80 // Parse du fichier config.yaml 81 err = yaml.Unmarshal(yamlFile, &lectureInit) 82 if err != nil { 83 fmt.Println("Erreur dans le parse du fichier config.yaml") 84 fmt.Println(err) 85 os.Exit(1) 86 } 87 88 http.HandleFunc(lectureInit.Raspberry.Url, func(w http.ResponseWriter, r *http.Request) { 89 api(w, r) 90 }) 91 92 fmt.Println("Server Stats listen port : " + lectureInit.Raspberry.Port) 93 http.ListenAndServe(":"+lectureInit.Raspberry.Port, nil) 94 } 95 96 // Fonction de réponse HTTP 97 func api(w http.ResponseWriter, r *http.Request) { 98 99 // Démarre chrono 100 start := time.Now().UTC() 101 102 // Demande les stats 103 percent, _ := cpu.Percent(time.Second, false) 104 memory, _ := mem.VirtualMemory() 105 106 // Calcul le temps pour lecture température 107 elapse := time.Since(start) 108 elapseSeconds := fmt.Sprintf("%.3f", elapse.Seconds()) 109 110 // Température CPU 111 re := regexp.MustCompile("\\d+(\\.\\d+)?") 112 // 50464 113 out, err := exec.Command("cat", "/sys/class/thermal/thermal_zone0/temp").Output() 114 if err != nil { 115 fmt.Println(err) 116 } 117 temperature, err := strconv.ParseFloat(re.FindString(string(out)), 64) 118 if err != nil { 119 fmt.Println("cmd err") 120 } 121 122 temperatureDegrees := strconv.FormatFloat(temperature/1000, 'f', 2, 64) 123 124 // Fréquence CPU 125 out, err = exec.Command("cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq").Output() 126 if err != nil { 127 fmt.Println(err) 128 } 129 frequence, err := strconv.ParseFloat(re.FindString(string(out)), 64) 130 if err != nil { 131 fmt.Println("cmd err") 132 } 133 134 // Création du JSON 135 donnees := Stats{ 136 Type: "Stats", 137 Domos_poolhouse_localTechnique_Raspberry_Stats_Cpu: fmt.Sprintf("%.3f", percent[0]), 138 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Total: fmt.Sprintf("%v", memory.Total), 139 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Available: fmt.Sprintf("%v", memory.Available), 140 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Used: fmt.Sprintf("%v", memory.Used), 141 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_UsedPourcent: fmt.Sprintf("%v", memory.UsedPercent), 142 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_UsedFree: fmt.Sprintf("%v", memory.Free), 143 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_UsedActive: fmt.Sprintf("%v", memory.Active), 144 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_UsedInactive: fmt.Sprintf("%v", memory.Inactive), 145 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Wired: fmt.Sprintf("%v", memory.Wired), 146 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Buffers: fmt.Sprintf("%v", memory.Buffers), 147 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Cached: fmt.Sprintf("%v", memory.Cached), 148 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Writeback: fmt.Sprintf("%v", memory.Writeback), 149 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Dirty: fmt.Sprintf("%v", memory.Dirty), 150 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_WritebackTmp: fmt.Sprintf("%v", memory.WritebackTmp), 151 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Shared: fmt.Sprintf("%v", memory.Shared), 152 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_Slab: fmt.Sprintf("%v", memory.Slab), 153 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_PageTables: fmt.Sprintf("%v", memory.PageTables), 154 Domos_poolhouse_localTechnique_Raspberry_Stats_Mem_SwapCached: fmt.Sprintf("%v", memory.SwapCached), 155 Domos_poolhouse_localTechnique_Raspberry_Stats_Temperature: temperatureDegrees, 156 Domos_poolhouse_localTechnique_Raspberry_Stats_Frequence: fmt.Sprintf("%.3f", frequence), 157 Domos_poolhouse_localTechnique_Raspberry_Stats_Elapsed: elapseSeconds, 158 Version: version, 159 } 160 161 jsonData, _ := json.Marshal(donnees) 162 163 w.Header().Set("content-type", "application/json") 164 w.Write([]byte(jsonData)) 165 166 }