func mustMysqlGetTableFieldTypeList(TableName string) (out []MysqlField) { fieldRowList := MustQuery(`SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?`, GetDefaultDbConfig().DbName, TableName) for _, row := range fieldRowList { field := MysqlFieldType{} field.DataType = MysqlDataType(row["DATA_TYPE"]) field.Default = row["COLUMN_DEFAULT"] switch field.DataType { case MysqlDataTypeVarchar: field.CharacterSetName = row["CHARACTER_SET_NAME"] field.CollationName = row["COLLATION_NAME"] field.StringLength = kmgStrconv.AtoIDefault0(row["CHARACTER_MAXIMUM_LENGTH"]) case MysqlDataTypeLongText: field.CharacterSetName = row["CHARACTER_SET_NAME"] field.CollationName = row["COLLATION_NAME"] case MysqlDataTypeInt32, MysqlDataTypeInt8: field.IsUnsigned = strings.Contains(row["COLUMN_TYPE"], "unsigned") field.IsAutoIncrement = strings.Contains(row["EXTRA"], "auto_increment") case MysqlDataTypeDateTime, MysqlDataTypeFloat, MysqlDataTypeLongBlob: default: panic(fmt.Errorf("TODO implement MysqlDataType %s", field.DataType)) } out = append(out, MysqlField{ Name: row["COLUMN_NAME"], Type: field, }) } return out }
func GetFirstIntFromRowList(rowList []map[string]string) int { if len(rowList) == 0 { return 0 } for _, val := range rowList[0] { return kmgStrconv.AtoIDefault0(val) } return 0 }
func cpu(output string) (used float64, numOfCore int) { o := strings.Split(output, "\n") o0 := strings.TrimSuffix(o[0], " CPU)") ol0 := strings.Split(o0, "(") numOfCore = kmgStrconv.AtoIDefault0(ol0[len(ol0)-1]) o1 := strings.Split(o[3], " ") _used := kmgStrconv.ParseFloat64Default0(o1[len(o1)-1]) used = (float64(100) - _used) / float64(100) used = kmgMath.Float64RoundToRelativePrec(used, 4) return }
func memory(output string) (used float64, total int) { o := strings.Split(output, "\n") o0 := strings.TrimPrefix(o[2], "-/+ buffers/cache:") o1 := strings.Split(o0, " ") usedByte := 0 free := 0 for _, s := range o1 { if s == "" { continue } if usedByte == 0 { usedByte = kmgStrconv.AtoIDefault0(s) continue } free = kmgStrconv.AtoIDefault0(s) break } total = usedByte + free used = float64(usedByte) / float64(total) used = kmgMath.Float64RoundToRelativePrec(used, 4) return }
func networkRXTX(output string) (rx int, tx int) { al := strings.Split(output, "\n") sl := "" for _, v := range al { v = strings.TrimSpace(v) v = strings.TrimPrefix(v, "\t") if strings.HasPrefix(v, "RX bytes") { sl = v break } } l := strings.Split(sl, " ") _l := []string{} for _, s := range l { if s == "" || s == "\t" { continue } _l = append(_l, s) } rx = kmgStrconv.AtoIDefault0(strings.TrimPrefix(strings.TrimSuffix(strings.Split(_l[0], "(")[0], " "), "RX bytes:")) tx = kmgStrconv.AtoIDefault0(strings.TrimPrefix(strings.TrimSuffix(strings.Split(_l[1], "(")[0], " "), "TX bytes:")) return }
//psOutput 样例 // 1234 /bin/hi -l :1024 //12345 /bin/world -n test func Extract(psOutput string) []*Process { lines := strings.Split(psOutput, "\n") out := []*Process{} for _, l := range lines { ls := strings.Fields(l) if len(ls) == 0 { continue } p := &Process{ Id: kmgStrconv.AtoIDefault0(ls[0]), Command: strings.Join(ls[1:], " "), } p.StartCmd = "setsid " + p.Command out = append(out, p) } return out }
func ikeUserCount(output string) int { lines := strings.Split(output, "\n") c := "" if !strings.Contains(output, "IKE_SAs") { return 0 } for _, line := range lines { if strings.HasPrefix(line, "IKE_SAs") { _line := strings.Split(line, "total") c = _line[0] c = strings.Trim(c, "IKE_SAs:") c = strings.TrimSpace(c) break } } return kmgStrconv.AtoIDefault0(c) }
func (t1 MysqlFieldType) String() string { out := string(t1.DataType) if t1.StringLength != 0 { out += "(" + strconv.Itoa(t1.StringLength) + ")" } if t1.IsUnsigned { out += " unsigned" } if t1.IsAutoIncrement { out += " auto_increment" } if t1.CollationName != "" { out += " COLLATE " + t1.CollationName } switch t1.DataType { case MysqlDataTypeInt32, MysqlDataTypeInt8, MysqlDataTypeFloat: out += " DEFAULT " + strconv.Itoa(kmgStrconv.AtoIDefault0(t1.Default)) case MysqlDataTypeVarchar, MysqlDataTypeDateTime: out += " DEFAULT " + fmt.Sprintf("%#v", t1.Default) //TODO 正确的序列化方式 } return out }
func disk(output string) (used float64, total int) { o := strings.Split(output, "\n") o0 := strings.Split(o[1], " ") after := 0 for i, s := range o0 { if i == 0 { continue } if s == "" { continue } if total == 0 { total = kmgStrconv.AtoIDefault0(s) continue } after++ if after == 3 { p := kmgStrconv.ParseFloat64Default0(strings.TrimSuffix(s, "%")) used = kmgMath.Float64RoundToRelativePrec(p/float64(100), 4) break } } return }
func networkConnection(output string) (connectionCount int) { output = strings.TrimSpace(output) return kmgStrconv.AtoIDefault0(output) }