unstick r1836413

Revision 1836413

Date:
2018/07/21 16:42:17
Author:
jpgilaberte
Revision Log:
Initial commit
Files:

Legend:

 
Added
 
Removed
 
Modified
  • labs/turbulence/agent.go

     
    1 package main
    2
    3
    4 type agent struct{
    5 cgroupPaths []string
    6 ppidConatiner []string
    7 }
    8
    9 type process struct{
    10 id string
    11 pid string
    12 name string
    13 }
    14
    15 var agentSingletonInstance *agent
    16
    17 func NewAgent()(*agent){
    18 if nil == agentSingletonInstance{
    19 agentSingletonInstance = new(agent)
    20 }
    21 return agentSingletonInstance
    22 }
    23
    24 func (a *agent) PopulateCgroupPaths()([]string, error){
    25 var err error
    26 a.cgroupPaths, err = FindCgroupPaths([]string{cgroupFreezerMesosPath, cgroupFreezerDockerPath})
    27 return a.cgroupPaths, err
    28 }
    29
    30 func (a *agent) PopulatePPidContainer(paths []string)([]string, error){
    31 var err error
    32 a.ppidConatiner, err = GetProcessFromCgroupPaths(paths)
    33 return a.ppidConatiner, err
    34 }
    35
  • labs/turbulence/container.go

     
    1 package main
    2
    3 import (
    4 "io/ioutil"
    5 "fmt"
    6 "bufio"
    7 "os"
    8 "github.com/pkg/errors"
    9 "bytes"
    10 )
    11
    12 type Container interface {
    13 FindCgroupPaths(path string)(cgroupPaths []string, err error)
    14 }
    15
    16 func FindCgroupPaths(paths []string)(cgroupPaths []string, err error){
    17 var auxCgroupPaths []string
    18 for _, path := range paths {
    19 auxCgroupPaths, _ = FindCgroupPath(path)
    20 for _, auxCgroupPath := range auxCgroupPaths{
    21 cgroupPaths = append(cgroupPaths, auxCgroupPath)
    22 }
    23 }
    24 return
    25 }
    26
    27 func FindCgroupPath(path string)(cgroupPaths []string, err error){
    28 dir, err := ioutil.ReadDir(path)
    29 if err != nil {
    30 fmt.Printf("ERROR - FindCgroupPaths - root path is not present - %v \n", err)
    31 }
    32 for _, f := range dir {
    33 if f.IsDir(){
    34 fmt.Printf("INFO - FindCgroupPaths - Add new cgroup path - %v \n", fmt.Sprint(cgroupFreezerMesosPath, f.Name()))
    35 cgroupPaths = append(cgroupPaths, fmt.Sprint(cgroupFreezerMesosPath, f.Name()))
    36 }
    37 }
    38 return
    39 }
    40
    41 func GetProcessFromCgroupPaths(paths []string)(cgroupPaths []string, err error){
    42 var auxProcessContainer []string
    43 for _, path := range paths {
    44 auxProcessContainer, _ = GetProcessFromCgroupPath(path)
    45 ppidCOnatiner, _ := PpidOfContainer(auxProcessContainer)
    46 cgroupPaths = append(cgroupPaths, ppidCOnatiner)
    47 }
    48 return
    49 }
    50
    51 func GetProcessFromCgroupPath(path string)(process []string, err error){
    52 fmt.Printf("INFO - GetProcessFromCgroupPaths - Path: %v \n", path)
    53 file, err := os.Open(path + "/cgroup.procs")
    54 if err != nil {
    55 fmt.Printf("ERROR - GetProcessFromCgroupPath - %v \n", err)
    56 return
    57 }
    58 defer file.Close()
    59 scanner := bufio.NewScanner(file)
    60 scanner.Split(bufio.ScanLines)
    61 for scanner.Scan() {
    62 err = scanner.Err()
    63 if err == nil {
    64 fmt.Printf("INFO - GetProcessFromCgroupPath - Container Pid: %v \n", scanner.Text())
    65 process = append(process, scanner.Text())
    66 }
    67 }
    68 return
    69 }
    70
    71 func PpidOfContainer(tasksContainer []string)(ppidCOntainer string, err error){
    72 for _, task := range tasksContainer {
    73 ppid, err := getPPID(task)
    74 if err == nil {
    75 if isPpidContainer(ppid, tasksContainer) {
    76 fmt.Printf("INFO - isPpidContainer - Ppid of container: %v \n", task)
    77 return task, err
    78 }
    79 }
    80 }
    81 fmt.Printf("ERROR - PpidOfContainer- Not find Ppid in container with process: %v \n", tasksContainer)
    82 return "", errors.New("Not find Ppid")
    83 }
    84
    85 func isPpidContainer(pid string, tasks []string)(bool){
    86 for _, task := range tasks {
    87 if pid == task {
    88 return false
    89 }
    90 }
    91 return true
    92 }
    93
    94 func getPPID(task string)(ppid string, err error){
    95 file, err := os.Open(fmt.Sprintf("/proc/%s/stat", task))
    96 if err != nil {
    97 fmt.Printf("ERROR - getPPID - %v \n", err)
    98 return ppid, err
    99 }
    100 defer file.Close()
    101 data, err := ioutil.ReadAll(file)
    102 if err != nil {
    103 fmt.Printf("ERROR - getPPID - %v \n", err)
    104 return ppid, err
    105 }
    106 _, err = fmt.Fscan(bytes.NewBuffer(data), &ppid, &ppid, &ppid, &ppid)
    107 fmt.Printf("INFO - getPPID - Ppid of task: %v PPid: %v \n", task, ppid)
    108 return ppid, err
    109 }
  • labs/turbulence/docker_container.go

     
    1 package main
    2
    3 const(
    4 cgroupFreezerDockerPath string = "/sys/fs/cgroup/freezer/docker/"
    5 )
    6
    7 func FindDockerCgroupPaths()(cgroupPaths []string, err error){
    8 return FindCgroupPath(cgroupFreezerDockerPath)
    9 }
    10
  • labs/turbulence/mesos_container.go

     
    1 package main
    2
    3 const(
    4 cgroupFreezerMesosPath string = "/tmp/mesos/"
    5 )
    6
    7 func FindMesosCgroupPaths()(cgroupPaths []string, err error){
    8 return FindCgroupPath(cgroupFreezerMesosPath)
    9 }
  • labs/turbulence/stat.go

     
    1 package main
    2
    3 type stat struct {
    4
    5 }
    6
    7
  • labs/turbulence/turbulence.go

     
    1 package main
    2
    3 import "fmt"
    4
    5 func main(){
    6
    7 var agent = NewAgent()
    8 agent.PopulateCgroupPaths()
    9 agent.PopulatePPidContainer(agent.cgroupPaths)
    10 fmt.Printf("--- %v", agent.ppidConatiner)
    11
    12 }
    13
    14
  • labs/turbulence/turbulence_test.go

     
    1 package main
    2
    3 import "testing"
    4
    5 func TestTurbulence(t *testing.T){
    6 //ppidOfContainer([]string{"245", "285", "1234", "1233", "asd", "12345234", "1"})
    7 }
    8
    9 func BenchmarkTurbulence(b *testing.B){
    10 //ppidOfContainer([]string{"245", "285", "1234", "1233", "asd", "12345234", "1"})
    11 }
    12