예제 #1
0
func main() {
	flag.Parse() // Scan the arguments list

	if versionFlag {
		fmt.Println("Version:", APP_VERSION)
	} else {
		// Fill InfluxDB connection settings
		var client *influxClient.Client = nil
		if databaseFlag != "" {
			config := new(influxClient.ClientConfig)

			config.Host = hostFlag
			config.Username = usernameFlag
			config.Password = passwordFlag
			config.Database = databaseFlag

			var err error
			client, err = influxClient.NewClient(config)

			if err != nil {
				panic(err)
			}
		}

		// Build collect list
		var collectList []GatherFunc
		for _, c := range strings.Split(collectFlag, ",") {
			switch strings.Trim(c, " ") {
			case "cpus":
				collectList = append(collectList, cpus)
			case "mem":
				collectList = append(collectList, mem)
			case "swap":
				collectList = append(collectList, swap)
			case "uptime":
				collectList = append(collectList, uptime)
			case "load":
				collectList = append(collectList, load)
			case "network":
				collectList = append(collectList, network)
			case "disks":
				collectList = append(collectList, disks)
			default:
				fmt.Fprintf(os.Stderr, "Unknown collect option `%s'\n", c)
				return
			}
		}

		if prefixFlag != "" && prefixFlag[len(prefixFlag)-1] != '.' {
			prefixFlag += "."
		}

		ch := make(chan *influxClient.Series, len(collectList))

		// Without daemon mode, do at least one lap
		first := true

		for first || daemonFlag {
			first = false

			// Collect data
			var data []*influxClient.Series

			for _, cl := range collectList {
				go cl(prefixFlag, ch)
			}

			for i := len(collectList); i > 0; i-- {
				res := <-ch
				if res != nil {
					data = append(data, res)
				} else if !daemonFlag {
					// Loop if we haven't all data:
					// Since diffed data didn't respond the
					// first time they are collected, loop
					// one more time to have it
					first = true
				}
			}

			// Show data
			if !first && (databaseFlag == "" || verboseFlag != "") {
				if strings.ToLower(verboseFlag) == "text" || verboseFlag == "" {
					prettyPrinter(data)
				} else {
					b, _ := json.Marshal(data)
					fmt.Printf("%s\n", b)
				}
			}
			// Send data
			if client != nil && !first {
				if err := send(client, data); err != nil {
					panic(err)
				}
			}

			if daemonFlag || first {
				time.Sleep(daemonIntervalFlag)
			}
		}
	}
}
예제 #2
0
func main() {
	if len(os.Args) < 2 {
		fail(errors.New("No argument file receive"))
	}

	args := parseConf(os.Args[1])

	config := influxClient.ClientConfig{}

	if v, present := args["login_host"]; present {
		config.Host = v
	}
	if v, present := args["login_username"]; present {
		config.Username = v
	}
	if v, present := args["login_password"]; present {
		config.Password = v
	}

	client, err := influxClient.NewClient(&config)
	if err != nil {
		fail(err)
	}

	if uType, present := args["user_type"]; present {
		// Manage users

		var username string
		if username, present = args["username"]; !present {
			fail(errors.New("You have not given username parameter"))
		}
		password, _ := args["password"]

		user_exists := false
		ret := ReturnCh{false}

		if uType == "cluster_admin" {
			u, _ := client.GetClusterAdminList()
			for _, v := range u {
				if v["username"] == username {
					user_exists = true
					break
				}
			}

			if state, present := args["state"]; present && state == "absent" && user_exists {
				err := client.DeleteClusterAdmin(username)

				if err != nil {
					fail(err)
				} else {
					ret.Changed = true
				}
			} else if state, present := args["state"]; present && state == "present" {
				if !user_exists {
					err := client.CreateClusterAdmin(username, password)

					if err != nil {
						fail(err)
					} else {
						ret.Changed = true
					}
				} else if password != "" {
					client.UpdateClusterAdmin(username, password)
				}
			}

		} else if dbname, present := args["database"]; present && uType == "user" {
			u, _ := client.GetDatabaseUserList(dbname)
			for _, v := range u {
				if v["name"] == username {
					user_exists = true
					break
				}
			}

			if state, present := args["state"]; present && state == "absent" && user_exists {
				err := client.DeleteDatabaseUser(dbname, username)

				if err != nil {
					fail(err)
				} else {
					ret.Changed = true
				}
			} else if state, present := args["state"]; present && state == "present" {
				if !user_exists {
					err := client.CreateDatabaseUser(dbname, username, password)

					if err != nil {
						fail(err)
					} else {
						ret.Changed = true
					}
				} else if password != "" {
					client.UpdateDatabaseUser(dbname, username, password)
				}
			}
		}

		s, _ := json.Marshal(ret)
		fmt.Printf("%s\n", s)
	} else if dbname, present := args["database"]; present {
		// Manage databases

		db_exists := false
		ret := ReturnCh{false}

		dbs, _ := client.GetDatabaseList()
		for _, v := range dbs {
			if v["name"] == dbname {
				db_exists = true
				break
			}
		}

		if state, present := args["state"]; present && state == "absent" && db_exists {
			err := client.DeleteDatabase(dbname)

			if err != nil {
				fail(err)
			} else {
				ret.Changed = true
			}
		} else if state, present := args["state"]; present && state == "present" && !db_exists {
			var err error

			if replication, present := args["replication"]; present {
				// err = client.CreateReplicatedDatabase(dbname, replication)
				err = client.CreateDatabase(dbname)
				if err == nil {
					err = fmt.Errorf("Can't set replication factor (%d), not supported by client", replication)
				}
			} else {
				err = client.CreateDatabase(dbname)
			}

			if err != nil {
				fail(err)
			} else {
				ret.Changed = true
			}
		}

		s, _ := json.Marshal(ret)
		fmt.Printf("%s\n", s)
	} else {
		// Gathering facts

		gprefix := "influxdb"
		if v, present := args["facts_prefix"]; present {
			gprefix = v
		}

		ret := ReturnFa{false, make(map[string]interface{})}

		dbs, _ := client.GetDatabaseList()
		ret.Facts[gprefix+"_dbs"] = dbs

		adms, _ := client.GetClusterAdminList()
		ret.Facts[gprefix+"_cluster_admins"] = adms

		for _, db := range dbs {
			dbname := db["name"].(string)
			users, _ := client.GetDatabaseUserList(dbname)
			ret.Facts[gprefix+"_users_"+dbname] = users
		}

		s, _ := json.Marshal(ret)
		fmt.Printf("%s\n", s)
	}

	//	s, err := json.Marshal(m) // produce JSON from Time struct
	//	if err != nil {
	//		fmt.Println(err)
	//	}else{
	//		fmt.Printf("%s", s)
	//	}
}