SHARE
TWEET

Untitled

a guest Jun 18th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "time"
  6.     //"encoding/json"
  7.    
  8.     "github.com/shirou/gopsutil/disk"
  9.     "github.com/shirou/gopsutil/load"
  10.     "github.com/shirou/gopsutil/mem"
  11.     "github.com/esvm/dcm-middleware/distribution/models"
  12.     "github.com/esvm/dcm-middleware/dcm"
  13.     "github.com/esvm/dcm-middleware/scylla"
  14.     //"github.com/streadway/amqp"
  15. )
  16.  
  17. var (
  18.     loadCPUTopic *models.Topic
  19.     memUsedTopic *models.Topic
  20.     diskUsedTopic *models.Topic
  21. )
  22.  
  23. const host = "ec2-18-231-106-190.sa-east-1.compute.amazonaws.com"
  24.  
  25. type data struct {
  26.     loadCPU5 float64
  27.     memUsed  float64
  28.     diskUsed float64
  29. }
  30.  
  31. func getInfoHW() data {
  32.     time.Sleep(200 * time.Millisecond)
  33.     m, _ := mem.VirtualMemory()
  34.     l, _ := load.Avg()
  35.     d, _ := disk.Usage("/")
  36.    
  37.     return data{
  38.         loadCPU5: l.Load5,
  39.         memUsed: m.UsedPercent,
  40.         diskUsed: d.UsedPercent,
  41.     }
  42. }
  43.  
  44. func publish(c *dcm.Connection, topic *models.Topic, payload float64) {
  45.     message := &models.Message{
  46.         TopicID: topic.ID,
  47.         Payload: payload,
  48.     }
  49.  
  50.     err := c.Publish(topic.ID, message)
  51.     if err != nil {
  52.         fmt.Println(err)
  53.     }
  54. }
  55.  
  56. func createTopics(c *dcm.Connection) error {
  57.     var err error
  58.     loadCPUTopic, err = c.CreateTopic("load_cpu", models.TopicProperties{IndexName: "load_cpu_index", StartFrom: scylla.Begin})
  59.     if err != nil {
  60.         return err
  61.     }
  62.    
  63.     memUsedTopic, err = c.CreateTopic("mem_used", models.TopicProperties{IndexName: "mem_used_index", StartFrom: scylla.Begin})
  64.     if err != nil {
  65.         return err
  66.     }
  67.    
  68.     diskUsedTopic, err = c.CreateTopic("disk_used", models.TopicProperties{IndexName: "disk_used_index", StartFrom: scylla.Begin})
  69.     if err != nil {
  70.         return err
  71.     }
  72.    
  73.     return nil
  74. }
  75.  
  76. /*func createTopicsRabbitMQ(ch *amqp.Channel) {
  77.     ch.QueueDeclare("load_cpu", false, false, false, false, nil)
  78.     ch.QueueDeclare("mem_used", false, false, false, false, nil)
  79.     ch.QueueDeclare("disk_used", false, false, false, false, nil)
  80. }
  81.  
  82. func publishRabbitMQ(ch *amqp.Channel, name string, value float64) {
  83.     body, _ := json.Marshal(value)
  84.     ch.Publish("",
  85.         name,
  86.         false,
  87.         false,
  88.         amqp.Publishing{
  89.         ContentType: "application/json",
  90.         Body: body,
  91.     })
  92. }*/
  93.  
  94. func main() {
  95.     c, err := dcm.Connect(host, 8426, 10)
  96.     defer c.Close()
  97.    
  98.     err = createTopics(c)
  99.     if err != nil {
  100.         fmt.Println(err)
  101.         return
  102.     }
  103.    
  104.     /*conn, err := amqp.Dial("amqp://guest:guest@ec2-18-231-106-190.sa-east-1.compute.amazonaws.com:5672")
  105.      if err != nil {
  106.         return
  107.      }
  108.  
  109.     ch, err := conn.Channel()
  110.     createTopicsRabbitMQ(ch)*/
  111.    
  112.     for {
  113.         dataHW := getInfoHW()
  114.         publish(c, loadCPUTopic, dataHW.loadCPU5)
  115.         publish(c, memUsedTopic, dataHW.memUsed)
  116.         publish(c, diskUsedTopic, dataHW.diskUsed)
  117.         /*publishRabbitMQ(ch, "load_cpu", dataHW.loadCPU5)
  118.         publishRabbitMQ(ch, "mem_used", dataHW.memUsed)
  119.         publishRabbitMQ(ch, "disk_used", dataHW.diskUsed)*/
  120.     }
  121. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top