Gathering stats about containers
This commit is contained in:
		
							parent
							
								
									57c0ddd71d
								
							
						
					
					
						commit
						31cdc6ef42
					
				
					 5 changed files with 77 additions and 8 deletions
				
			
		
							
								
								
									
										14
									
								
								apps/main.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								apps/main.go
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -111,6 +111,20 @@ func Update(name string, SSHPort int, HTTPPort int, image string, CPU int, memor
 | 
			
		|||
	return &app, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateState sets state
 | 
			
		||||
func UpdateState(name string, state string, CPUUsage float64, memory int, diskUsageBytes int, diskUsageInodes int) error {
 | 
			
		||||
	db := common.GetDBConnection()
 | 
			
		||||
 | 
			
		||||
	err := db.Model(&App{}).Updates(App{
 | 
			
		||||
		State:           state,
 | 
			
		||||
		CPUUsage:        CPUUsage,
 | 
			
		||||
		MemoryUsage:     memory,
 | 
			
		||||
		DiskUsageBytes:  diskUsageBytes,
 | 
			
		||||
		DiskUsageInodes: diskUsageInodes,
 | 
			
		||||
	}).Where("name = ?", name).Error
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Delete removes records about one app from the database
 | 
			
		||||
func Delete(name string) error {
 | 
			
		||||
	db := common.GetDBConnection()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,8 +3,8 @@ package apps
 | 
			
		|||
import (
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/jinzhu/gorm"
 | 
			
		||||
	// This is line from GORM documentation that imports database dialect
 | 
			
		||||
	_ "github.com/jinzhu/gorm/dialects/sqlite"
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			@ -29,12 +29,15 @@ type AppState struct {
 | 
			
		|||
	CPUUsage        float64 `json:"cpu_usage"`    // in percents
 | 
			
		||||
	MemoryUsage     int     `json:"memory_usage"` // in MB
 | 
			
		||||
	DiskUsageBytes  int     `json:"disk_usage_bytes"`
 | 
			
		||||
	DiskUsageinodes int     `json:"disk_usage_inodes"`
 | 
			
		||||
	DiskUsageInodes int     `json:"disk_usage_inodes"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// App keeps info about hosted application
 | 
			
		||||
type App struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	ID        uint       `gorm:"primary_key" json:"id"`
 | 
			
		||||
	CreatedAt time.Time  `json:"created_at"`
 | 
			
		||||
	UpdatedAt time.Time  `json:"updated_at"`
 | 
			
		||||
	DeletedAt *time.Time `sql:"index" json:"deleted_at"`
 | 
			
		||||
 | 
			
		||||
	Name     string `json:"name" gorm:"primary_key"`
 | 
			
		||||
	SSHPort  int    `json:"ssh_port"`
 | 
			
		||||
| 
						 | 
				
			
			@ -48,7 +51,7 @@ type App struct {
 | 
			
		|||
	CPUUsage        float64 `json:"cpu_usage"`    // in percents
 | 
			
		||||
	MemoryUsage     int     `json:"memory_usage"` // in MB
 | 
			
		||||
	DiskUsageBytes  int     `json:"disk_usage_bytes"`
 | 
			
		||||
	DiskUsageinodes int     `json:"disk_usage_inodes"`
 | 
			
		||||
	DiskUsageInodes int     `json:"disk_usage_inodes"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Validate do basic checks of the struct values
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,8 +22,8 @@ func (c *Container) volumeHostPath() string {
 | 
			
		|||
	return path.Join("/srv", c.App.Name)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetApp app object with populated state fields
 | 
			
		||||
func (c *Container) GetApp() (*apps.AppState, error) {
 | 
			
		||||
// GetState app object with populated state fields
 | 
			
		||||
func (c *Container) GetState() (*apps.AppState, error) {
 | 
			
		||||
	status, err := c.Status()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
| 
						 | 
				
			
			@ -44,7 +44,7 @@ func (c *Container) GetApp() (*apps.AppState, error) {
 | 
			
		|||
		CPUUsage:        cpu,
 | 
			
		||||
		MemoryUsage:     memory,
 | 
			
		||||
		DiskUsageBytes:  bytes,
 | 
			
		||||
		DiskUsageinodes: inodes,
 | 
			
		||||
		DiskUsageInodes: inodes,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &state, nil
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										12
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								main.go
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -3,6 +3,7 @@ package main
 | 
			
		|||
import (
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/labstack/echo"
 | 
			
		||||
	"github.com/rosti-cz/node-api/apps"
 | 
			
		||||
| 
						 | 
				
			
			@ -24,6 +25,15 @@ func main() {
 | 
			
		|||
	// Templating
 | 
			
		||||
	t := &Template{}
 | 
			
		||||
 | 
			
		||||
	// Stats loop
 | 
			
		||||
	go func() {
 | 
			
		||||
		err := gatherContainerStats()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("LOOP ERROR:", err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		time.Sleep(5 * time.Minute)
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	// API
 | 
			
		||||
	e := echo.New()
 | 
			
		||||
	e.Renderer = t
 | 
			
		||||
| 
						 | 
				
			
			@ -283,5 +293,5 @@ func main() {
 | 
			
		|||
		return c.JSON(http.StatusOK, node)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	e.Logger.Fatal(e.Start(":1323"))
 | 
			
		||||
	e.Logger.Fatal(e.Start("127.0.0.1:1323"))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										42
									
								
								stats.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								stats.go
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,42 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"log"
 | 
			
		||||
 | 
			
		||||
	"github.com/rosti-cz/node-api/apps"
 | 
			
		||||
	"github.com/rosti-cz/node-api/docker"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// gatherContainerStats gathers information about containers and saves it into the database
 | 
			
		||||
func gatherContainerStats() error {
 | 
			
		||||
	appList, err := apps.List()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, app := range *appList {
 | 
			
		||||
		container := docker.Container{
 | 
			
		||||
			App: &app,
 | 
			
		||||
		}
 | 
			
		||||
		state, err := container.GetState()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("STATS ERROR:", err.Error())
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		err = apps.UpdateState(
 | 
			
		||||
			app.Name,
 | 
			
		||||
			state.State,
 | 
			
		||||
			state.CPUUsage,
 | 
			
		||||
			state.MemoryUsage,
 | 
			
		||||
			state.DiskUsageBytes,
 | 
			
		||||
			state.DiskUsageInodes,
 | 
			
		||||
		)
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("STATS ERROR:", err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in a new issue