update restapi of resource_monitor
authorwansuyoo <wansu.yoo@samsung.com>
Fri, 8 Mar 2019 07:39:09 +0000 (16:39 +0900)
committerwansuyoo <wansu.yoo@samsung.com>
Fri, 8 Mar 2019 07:39:09 +0000 (16:39 +0900)
doc/edge-orchestration-api.json [changed mode: 0755->0644]
src/devicemgr/main.go [new file with mode: 0644]
src/devicemgr/resource_monitor.go
src/orchestration/main.go
src/restapi/v1/restapi.go [new file with mode: 0644]
src/restapi/v1/routers.go
src/restapi/v1/types.go [new file with mode: 0644]
src/servicemgr/service_execute.go

old mode 100755 (executable)
new mode 100644 (file)
diff --git a/src/devicemgr/main.go b/src/devicemgr/main.go
new file mode 100644 (file)
index 0000000..6bb313d
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Edge Orchestration
+ *
+ * Edge Orchestration support to deliver distributed service process environment.
+ *
+ * API version: v1-20190307
+ * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
+ */
+
+package devicemgr
+
+import (
+       "log"
+)
+
+// InitDeviceMgr function
+func InitDeviceMgr() {
+       log.Printf("[%s] InitDeviceMgr", logPrefix)
+
+       // start go routines for sampling H/W resource
+       go samplingProcStat(30)
+       go samplingNetworkUsage()
+       go samplingDiskUsage()
+}
+
+// GetGeneralUsageCPU function
+func GetGeneralUsageCPU() float64 {
+       return getUsageCPU()
+}
+
+// GetUsageMemory function
+func GetUsageMemory() float64 {
+       return getUsageMemory()
+}
+
+// GetUsageNetwork function
+func GetUsageNetwork() float64 {
+       return getUsageNetwork()
+}
+
+// GetUsageDisk function
+func GetUsageDisk() float64 {
+       return getUsageDisk()
+}
index e4a0113..fd2b571 100644 (file)
 package devicemgr
 
 import (
-       "log"
-       "net/http"
+       "fmt"
+       "io/ioutil"
+       "strconv"
+       "strings"
+       "time"
 )
 
-// APIV1DeviceResourceUsageCPUGet function
-func APIV1DeviceResourceUsageCPUGet(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1DeviceResourceUsageCPUGet", logPrefix)
+type argError struct {
+       prob string
+}
+
+func (e *argError) Error() string {
+       return fmt.Sprintf("%s", e.prob)
+}
+
+//================================ CPU Usage ================================
+
+/*
+
+
+       $ cat /proc/stat
+       cpu  542368 14095 166906 800666582 273644 0 4972 0 0 0
+       cpu0 129538 944 19348 100063536 7308 0 762 0 0 0
+       cpu1 66523 1265 20022 100123890 9679 0 674 0 0 0
+       cpu2 43494 874 19086 100153938 4139 0 391 0 0 0
+       cpu3 101144 804 20314 100088439 11625 0 124 0 0 0
+       cpu4 47182 2101 18928 100145633 6619 0 141 0 0 0
+       cpu5 45282 1470 19422 100089955 6198 0 118 0 0 0
+       cpu6 60139 4839 21580 99909671 218700 0 302 0 0 0
+       cpu7 49062 1796 28203 100091517 9373 0 2458 0 0 0
+
+
+
+       http://www.linuxhowtos.org/System/procstat.htm
+       cpu num   user  nice-user      system      idle      iowait   irq  softirq
+       cpu       8975  0              14722       24534211  1294     0    530
+
+       cpuUsage = (all of processor jiffies - all of processor idle jiffies) / (all of processor jiffies)
+
+       cpuUsage = cpu0(100%) / (cpu0(100%) + ... + cpu7(0%)) = 12.5%
+
+*/
+
+func procStat() (idle, total uint64) {
+       contents, _ := ioutil.ReadFile("/proc/stat")
+
+       //parsing 'cpu' line
+       lines := strings.Split(string(contents), "\n")
+
+       for _, line := range lines {
+               fields := strings.Fields(line)
+
+               if fields[0] == "cpu" {
+
+                       numFields := len(fields)
+
+                       for i := 1; i < numFields; i++ {
+                               val, _ := strconv.ParseUint(fields[i], 10, 64)
+
+                               if i == 4 {
+                                       idle = val
+                               }
+
+                               total += val
+                       }
+
+                       return
+               }
+       }
+
+       return
+}
+
+var prevIdle uint64
+var prevTotal uint64
+var cpuUsage float64
+
+func samplingProcStat(frame uint64) {
+
+       prevIdle, prevTotal = procStat()
+
+       for true {
+
+               time.Sleep(time.Duration(60/frame) * time.Second)
+
+               nextIdle, nextTotal := procStat()
+
+               idleTicks := float64(nextIdle - prevIdle)
+               totalTicks := float64(nextTotal - prevTotal)
+
+               cpuUsage = 100 * (totalTicks - idleTicks) / totalTicks
+
+               fmt.Println("cpu jiffies: ", prevIdle, nextIdle, prevTotal, nextTotal, idleTicks, totalTicks)
+               fmt.Println("cpu usage: ", cpuUsage)
+
+               prevIdle = nextIdle
+               prevTotal = nextTotal
+       }
+}
+
+func getUsageCPU() float64 {
 
-       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
-       w.WriteHeader(http.StatusOK)
+       return cpuUsage
 }
 
-// APIV1DeviceResourceUsageDiskGet fucntion
-func APIV1DeviceResourceUsageDiskGet(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1DeviceResourceUsageDiskGet", logPrefix)
+func getIdleCPU() {
 
-       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
-       w.WriteHeader(http.StatusOK)
 }
 
-// APIV1DeviceResourceUsageMemoryGet function
-func APIV1DeviceResourceUsageMemoryGet(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1DeviceResourceUsageMemoryGet", logPrefix)
+func getUserUsageCPU() {
 
-       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
-       w.WriteHeader(http.StatusOK)
 }
 
-// APIV1DeviceResourceUsageNetworkGet function
-func APIV1DeviceResourceUsageNetworkGet(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1DeviceResourceUsageNetworkGet", logPrefix)
+func getNiceUserUsageCPU() {
 
-       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
-       w.WriteHeader(http.StatusOK)
 }
+
+func getSystemUsageCPU() {
+
+}
+
+func getIOwaitUsageCPU() {
+
+}
+
+func getIrqUsageCPU() {
+
+}
+
+func getSoftIrqUsageCPU() {
+
+}
+
+//================================ CPU Usage ================================
+
+//================================ MEM Usage ================================
+
+func getUsageMemory() float64 {
+
+       contents, _ := ioutil.ReadFile("/proc/meminfo")
+
+       //parsing 'cpu' line
+       lines := strings.Split(string(contents), "\n")
+
+       var memAvailable uint64
+       var memTotal uint64
+
+       var memUsage float64
+
+       for _, line := range lines {
+
+               // fmt.Println(line)
+
+               fields := strings.Fields(line)
+
+               numFields := len(fields)
+
+               // fmt.Println(numFields)
+
+               if numFields == 3 {
+                       // https://superuser.com/questions/980820/what-is-the-difference-between-memfree-and-memavailable-in-proc-meminfo
+                       if fields[0] == "MemAvailable:" {
+                               memAvailable, _ = strconv.ParseUint(fields[1], 10, 64)
+                       } else if fields[0] == "MemTotal:" {
+                               memTotal, _ = strconv.ParseUint(fields[1], 10, 64)
+                       }
+
+                       // fmt.Println(fields[0], "--", fields[1], "--", fields[2])
+
+               } else if numFields == 2 {
+
+               }
+
+       }
+
+       memUsage = 100 * float64(memTotal-memAvailable) / float64(memTotal)
+       fmt.Println("mem: ", memUsage)
+
+       return memUsage
+}
+
+//================================ MEM Usage ================================
+
+//================================ NET Usage ================================
+
+func getNetworkMBps() (recvTotal, tranTotal uint64) {
+
+       recvTotal = 0
+       tranTotal = 0
+
+       contents, _ := ioutil.ReadFile("/proc/net/dev")
+
+       lines := strings.Split(string(contents), "\n")
+
+       for lineNum, line := range lines {
+
+               // fmt.Println(line)
+               // fmt.Println(lineNum)
+
+               fields := strings.Fields(line)
+
+               numFields := len(fields)
+
+               if lineNum >= 2 && numFields == 17 {
+                       // fmt.Println(fields[1], "#", fields[9])
+
+                       recv_val, _ := strconv.ParseUint(fields[1], 10, 64)
+                       recvTotal += recv_val
+
+                       tran_val, _ := strconv.ParseUint(fields[9], 10, 64)
+                       tranTotal += tran_val
+               }
+       }
+
+       return
+}
+
+var networkMBps float64
+
+func samplingNetworkUsage() {
+
+       var prevRecv uint64
+       var prevTran uint64
+
+       var nextRecv uint64
+       var nextTran uint64
+
+       prevRecv, prevTran = getNetworkMBps()
+
+       for true {
+
+               time.Sleep(1 * time.Second)
+
+               nextRecv, nextTran = getNetworkMBps()
+
+               diffRecv := float64(nextRecv - prevRecv)
+               diffTran := float64(nextTran - prevTran)
+
+               networkMBps = (diffRecv + diffTran) / 1024 / 1024
+
+               fmt.Println("net : ", networkMBps)
+
+               prevRecv = nextRecv
+               prevTran = nextTran
+
+       }
+
+}
+
+func getUsageNetwork() float64 {
+       return networkMBps
+}
+
+//================================ NET Usage ================================
+
+//================================ DISK Usage ================================
+
+func getDiskByteIO() (readingByteTotal, writingByteTotal uint64) {
+
+       readingByteTotal = 0
+       writingByteTotal = 0
+
+       contents, _ := ioutil.ReadFile("/proc/diskstats")
+
+       lines := strings.Split(string(contents), "\n")
+
+       for _, line := range lines {
+
+               // fmt.Println(line)
+               // fmt.Println(lineNum)
+
+               fields := strings.Fields(line)
+
+               numFields := len(fields)
+
+               if numFields < 14 {
+                       continue
+               }
+
+               name := fields[2]
+
+               //exception 1
+               if strings.Contains(name, "loop") {
+                       continue
+               }
+
+               //exception 2
+               if strings.Contains(name, "ram") {
+                       continue
+               }
+
+               //exception 3
+               if strings.Contains(name, "sr") {
+                       continue
+               }
+
+               // major_sector, _ := strconv.ParseUint(fields[0], 10, 64)
+               minor_sector, _ := strconv.ParseUint(fields[1], 10, 64)
+
+               if minor_sector == 0 {
+
+                       read_sector, _ := strconv.ParseUint(fields[5], 10, 64)
+                       write_sector, _ := strconv.ParseUint(fields[9], 10, 64)
+
+                       sys_file_for_sector := fmt.Sprintf("/sys/block/%s/queue/hw_sector_size", name)
+
+                       sector_size_content, _ := ioutil.ReadFile(sys_file_for_sector)
+                       sector_size_lines := strings.Split(string(sector_size_content), "\n")
+
+                       sector_size, _ := strconv.ParseUint(sector_size_lines[0], 10, 64)
+
+                       readingByteTotal += read_sector * sector_size
+                       writingByteTotal += write_sector * sector_size
+
+               }
+
+       }
+
+       return
+}
+
+var diskMBps float64
+
+func samplingDiskUsage() {
+
+       var prevWriting uint64
+       var prevReading uint64
+
+       var nextWriting uint64
+       var nextReading uint64
+
+       prevWriting, prevReading = getDiskByteIO()
+
+       for true {
+
+               time.Sleep(1 * time.Second)
+
+               nextWriting, nextReading = getDiskByteIO()
+
+               diffWriting := float64(nextWriting - prevWriting)
+               diffReading := float64(nextReading - prevReading)
+
+               prevWriting = nextWriting
+               prevReading = nextReading
+
+               diskMBps = (diffWriting + diffReading) / 1024 / 1024
+
+               fmt.Println("disk : ", diskMBps)
+
+       }
+
+       return
+}
+
+func getUsageDisk() float64 {
+       return diskMBps
+}
+
+//================================ DISK Usage ================================
index 7b62857..050b4e5 100644 (file)
@@ -10,6 +10,7 @@
 package main
 
 import (
+       "devicemgr"
        "log"
        "net/http"
        restapi "restapi/v1"
@@ -18,6 +19,8 @@ import (
 func main() {
        log.Printf("[%s] Server started", logPrefix)
 
+       devicemgr.InitDeviceMgr()
+
        router := restapi.NewRouter()
 
        log.Fatal(http.ListenAndServe(":9090", router))
diff --git a/src/restapi/v1/restapi.go b/src/restapi/v1/restapi.go
new file mode 100644 (file)
index 0000000..da4bef2
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Edge Orchestration
+ *
+ * Edge Orchestration support to deliver distributed service process environment.
+ *
+ * API version: v1-20190307
+ * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
+ */
+
+package v1
+
+import (
+       "devicemgr"
+       "encoding/json"
+       "log"
+       "net/http"
+)
+
+// APIV1DeviceResourceUsageCPUGet function
+func APIV1DeviceResourceUsageCPUGet(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1DeviceResourceUsageCPUGet", logPrefix)
+
+       usage := devicemgr.GetGeneralUsageCPU()
+       data, _ := json.Marshal(CPUObject{CPUUsage: usage})
+       writeJSONResponse(w, data, http.StatusOK)
+}
+
+// APIV1DeviceResourceUsageDiskGet fucntion
+func APIV1DeviceResourceUsageDiskGet(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1DeviceResourceUsageDiskGet", logPrefix)
+
+       usage := devicemgr.GetUsageDisk()
+       data, _ := json.Marshal(DiskObject{DiskUsage: usage})
+       writeJSONResponse(w, data, http.StatusOK)
+}
+
+// APIV1DeviceResourceUsageMemoryGet function
+func APIV1DeviceResourceUsageMemoryGet(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1DeviceResourceUsageMemoryGet", logPrefix)
+
+       usage := devicemgr.GetUsageMemory()
+       data, _ := json.Marshal(MemoryObject{MemoryUsage: usage})
+       writeJSONResponse(w, data, http.StatusOK)
+}
+
+// APIV1DeviceResourceUsageNetworkGet function
+func APIV1DeviceResourceUsageNetworkGet(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1DeviceResourceUsageNetworkGet", logPrefix)
+
+       usage := devicemgr.GetUsageNetwork()
+       data, _ := json.Marshal(NetworkObject{NetworkUsage: usage})
+       writeJSONResponse(w, data, http.StatusOK)
+}
+
+// APIV1ServicemgrServiceDelete function
+func APIV1ServicemgrServiceDelete(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1ServicemgrServiceDelete", logPrefix)
+
+       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
+       w.WriteHeader(http.StatusOK)
+}
+
+// APIV1ServicemgrServiceGet function
+func APIV1ServicemgrServiceGet(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1ServicemgrServiceGet", logPrefix)
+
+       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
+       w.WriteHeader(http.StatusOK)
+}
+
+// APIV1ServicemgrServicePost function
+func APIV1ServicemgrServicePost(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1ServicemgrServicePost", logPrefix)
+
+       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
+       w.WriteHeader(http.StatusOK)
+}
+
+// APIV1ServicemgrServiceServiceIDGet function
+func APIV1ServicemgrServiceServiceIDGet(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1ServicemgrServiceServiceIDGet", logPrefix)
+
+       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
+       w.WriteHeader(http.StatusOK)
+}
+
+func writeJSONResponse(w http.ResponseWriter, data []byte, status int) {
+       log.Printf("[%s] writeJSONResponse: %s", logPrefix, data)
+       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
+       w.WriteHeader(status)
+       w.Write(data)
+}
index 3c3e1d6..50d1950 100644 (file)
@@ -14,9 +14,6 @@ import (
        "net/http"
        "strings"
 
-       "devicemgr"
-       "servicemgr"
-
        "github.com/gorilla/mux"
 )
 
@@ -66,55 +63,55 @@ var routes = Routes{
                "APIV1DeviceResourceUsageCPUGet",
                strings.ToUpper("Get"),
                "/api/v1/device/resource/usage/cpu",
-               devicemgr.APIV1DeviceResourceUsageCPUGet,
+               APIV1DeviceResourceUsageCPUGet,
        },
 
        Route{
                "APIV1DeviceResourceUsageDiskGet",
                strings.ToUpper("Get"),
                "/api/v1/device/resource/usage/disk",
-               devicemgr.APIV1DeviceResourceUsageDiskGet,
+               APIV1DeviceResourceUsageDiskGet,
        },
 
        Route{
                "APIV1DeviceResourceUsageMemoryGet",
                strings.ToUpper("Get"),
                "/api/v1/device/resource/usage/memory",
-               devicemgr.APIV1DeviceResourceUsageMemoryGet,
+               APIV1DeviceResourceUsageMemoryGet,
        },
 
        Route{
                "APIV1DeviceResourceUsageNetworkGet",
                strings.ToUpper("Get"),
                "/api/v1/device/resource/usage/network",
-               devicemgr.APIV1DeviceResourceUsageNetworkGet,
+               APIV1DeviceResourceUsageNetworkGet,
        },
 
        Route{
                "APIV1ServicemgrServiceDelete",
                strings.ToUpper("Delete"),
                "/api/v1/servicemgr/service",
-               servicemgr.APIV1ServicemgrServiceDelete,
+               APIV1ServicemgrServiceDelete,
        },
 
        Route{
                "APIV1ServicemgrServiceGet",
                strings.ToUpper("Get"),
                "/api/v1/servicemgr/service",
-               servicemgr.APIV1ServicemgrServiceGet,
+               APIV1ServicemgrServiceGet,
        },
 
        Route{
                "APIV1ServicemgrServicePost",
                strings.ToUpper("Post"),
                "/api/v1/servicemgr/service",
-               servicemgr.APIV1ServicemgrServicePost,
+               APIV1ServicemgrServicePost,
        },
 
        Route{
                "APIV1ServicemgrServiceServiceIDGet",
                strings.ToUpper("Get"),
                "/api/v1/servicemgr/service/{serviceID}",
-               servicemgr.APIV1ServicemgrServiceServiceIDGet,
+               APIV1ServicemgrServiceServiceIDGet,
        },
 }
diff --git a/src/restapi/v1/types.go b/src/restapi/v1/types.go
new file mode 100644 (file)
index 0000000..27d00b0
--- /dev/null
@@ -0,0 +1,23 @@
+package v1
+
+var logPrefix = "restapi/v1"
+
+// CPUObject struct
+type CPUObject struct {
+       CPUUsage float64 `json:"cpuUsage"`
+}
+
+// MemoryObject struct
+type MemoryObject struct {
+       MemoryUsage float64 `json:"memoryUsage"`
+}
+
+// NetworkObject struct
+type NetworkObject struct {
+       NetworkUsage float64 `json:"networkUsage"`
+}
+
+// DiskObject struct
+type DiskObject struct {
+       DiskUsage float64 `json:"diskUsage"`
+}
index 2ef4da7..1da7595 100644 (file)
@@ -8,40 +8,3 @@
  */
 
 package servicemgr
-
-import (
-       "log"
-       "net/http"
-)
-
-// APIV1ServicemgrServiceDelete function
-func APIV1ServicemgrServiceDelete(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1ServicemgrServiceDelete", logPrefix)
-
-       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
-       w.WriteHeader(http.StatusOK)
-}
-
-// APIV1ServicemgrServiceGet function
-func APIV1ServicemgrServiceGet(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1ServicemgrServiceGet", logPrefix)
-
-       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
-       w.WriteHeader(http.StatusOK)
-}
-
-// APIV1ServicemgrServicePost function
-func APIV1ServicemgrServicePost(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1ServicemgrServicePost", logPrefix)
-
-       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
-       w.WriteHeader(http.StatusOK)
-}
-
-// APIV1ServicemgrServiceServiceIDGet function
-func APIV1ServicemgrServiceServiceIDGet(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1ServicemgrServiceServiceIDGet", logPrefix)
-
-       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
-       w.WriteHeader(http.StatusOK)
-}