...

Source file src/golang.conradwood.net/tests/linux/testlinux.go

Documentation: golang.conradwood.net/tests/linux

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"golang.conradwood.net/go-easyops/linux"
     7  	"golang.conradwood.net/go-easyops/utils"
     8  	"os"
     9  	"strings"
    10  	"time"
    11  )
    12  
    13  var (
    14  	ds = flag.String("dirsize", "", "if set, calc dirsize")
    15  )
    16  
    17  func main() {
    18  	flag.Parse()
    19  	l := linux.New()
    20  	com := []string{"bash", "-c", "apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst|wc -l"}
    21  	out, err := l.SafelyExecute(com, nil)
    22  	fmt.Printf("Upgradable packages: \"%s\"\n", out)
    23  	utils.Bail("failed to count packages", err)
    24  	if *ds != "" {
    25  		size, err := linux.DirSize(*ds)
    26  		utils.Bail("failed to get dirsize", err)
    27  		fmt.Printf("Dirsize of \"%s\": %s\n", *ds, utils.PrettyNumber(size))
    28  		os.Exit(0)
    29  	}
    30  	allps, err := linux.AllPids()
    31  	utils.Bail("failed to get pids", err)
    32  	fmt.Printf("Got %d pids\n", len(allps))
    33  	for _, ps := range allps {
    34  		fmt.Printf("Pid: %s, Parent: %d, Cgroup: %s\n", ps, ps.ParentPid(), ps.Cgroup())
    35  	}
    36  
    37  	ps := linux.PidStatus(1)
    38  	printTreeOf(ps)
    39  	fmt.Printf("Pidstatus: %s\n", ps)
    40  	lin := linux.New()
    41  	fmt.Printf("My IP: %s\n", lin.MyIP())
    42  	run([]string{"/bin/true"})
    43  	check_with_duration(time.Duration(6)*time.Second, []string{"sleep", "3"})
    44  	check_with_duration(time.Duration(6)*time.Second, []string{"sleep", "100"})
    45  
    46  	run([]string{"sleep", "300"})
    47  	//time.Sleep(time.Duration(6) * time.Second)
    48  	//	TestExecuteContainer()
    49  }
    50  func run(com []string) {
    51  	fmt.Printf("executing \"%s\"...", strings.Join(com, " "))
    52  	lin := linux.New()
    53  	started := time.Now()
    54  	out, err := lin.SafelyExecute(com, nil)
    55  	if err != nil {
    56  		fmt.Printf("Output:\n%s\n", out)
    57  		utils.Bail("failed to execute", err)
    58  	}
    59  	fmt.Printf("Done (%0.1fs)\n", time.Since(started).Seconds())
    60  }
    61  func TestExecuteContainer() {
    62  	panic("no containers yet")
    63  }
    64  
    65  func copdir() {
    66  	err := linux.CopyDir("/tmp/x", "/tmp/y")
    67  	utils.Bail("failed to copydir", err)
    68  }
    69  
    70  func printTreeOf(ps *linux.ProcessState) {
    71  	printTree(ps, " ")
    72  }
    73  func printTree(ps *linux.ProcessState, prefix string) {
    74  	fmt.Printf("%s%s (Cgroup: \"%s\")\n", prefix, ps, ps.Cgroup())
    75  	children, err := ps.Children()
    76  	utils.Bail("failed to get children", err)
    77  	prefix = prefix + "   "
    78  	for _, c := range children {
    79  		//fmt.Printf("%s%s\n", prefix, c)
    80  		printTree(c, prefix)
    81  	}
    82  }
    83  

View as plain text