SHARE
TWEET

Untitled

a guest Jan 18th, 2020 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.   "flag"
  5.   "fmt"
  6.   "syscall"
  7.   "os"
  8.   "io/ioutil"
  9.   "os/user"
  10.   "database/sql"
  11.  
  12.   _ "github.com/go-sql-driver/mysql"
  13.   "gopkg.in/yaml.v2"
  14. )
  15.  
  16. // DbCredentials represents MySQL credentials. It is also part of yaml config`
  17. type DbCredentials struct {
  18.     Driver      string
  19.     User        string
  20.     Pass        string
  21.     Port        int
  22.     Host        string
  23.     Name        string
  24. }
  25.  
  26. // Index is part of yaml configuration
  27. type Index struct {
  28.     MinSize     int         `yaml:"min_size"`
  29.     IndexSum    []string    `yaml:"index_sum"`
  30. }
  31.  
  32. // Config is a root structure of yaml of config
  33. type Config struct {
  34.     Parameters  DbCredentials
  35.     Indexes     map[string]Index
  36.     User        string
  37.     Group       string
  38.     Files       []string
  39. }
  40.  
  41. func dbConnection(credentials *DbCredentials) (db *sql.DB) {
  42.     dns := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", credentials.User, credentials.Pass, credentials.Host, credentials.Port, credentials.Name)
  43.     db, err := sql.Open("mysql", dns)
  44.  
  45.     if err != nil {
  46.         panic(err.Error())
  47.     }
  48.  
  49.     return db
  50. }
  51.  
  52. func parseConfig(configPath string) Config {
  53.     configData, err := ioutil.ReadFile(configPath)
  54.     if err != nil {
  55.         panic("Cannot read config file from "+configPath)
  56.     }
  57.    
  58.     conf := Config{}
  59.     if err = yaml.Unmarshal([]byte(configData), &conf); err != nil {
  60.         panic(err)
  61.     }
  62.  
  63.     return conf
  64. }
  65.  
  66. func countIndex(db *sql.DB, name string) int {
  67.     rows, err := db.Query("SELECT COUNT(*) FROM "+name)
  68.     if err != nil {
  69.         panic(err)
  70.     }
  71.     defer rows.Close();
  72.  
  73.     for rows.Next() {
  74.         var num int
  75.         if err := rows.Scan(&num); err != nil {
  76.             panic(err)
  77.         }
  78.        
  79.         return num
  80.     }
  81.  
  82.     panic("Index not found")
  83. }
  84.  
  85. func checkIndexes(db *sql.DB, indexes *map[string]Index) {
  86.     count := map[string]int{}
  87.  
  88.     for k, v := range *indexes {
  89.         count[k] = countIndex(db, k)
  90.         if v.MinSize > 0 && count[k] < v.MinSize {
  91.             panic(fmt.Sprintf("Index %s is too small. Expected size: atleast %d. Given %d",
  92.                 k, v.MinSize, count[k]))
  93.         }
  94.     }
  95.  
  96.     var sum int
  97.     for k, v := range *indexes {
  98.         if len(v.IndexSum) <= 0 {
  99.             continue
  100.         }
  101.         sum = 0
  102.  
  103.         for _, item := range v.IndexSum {
  104.             if val, ok := count[item]; ok {
  105.                 sum += val
  106.                 continue
  107.             }
  108.  
  109.             panic(fmt.Sprintf("Index %s required to compute sum for %s is not defined.",
  110.                 item, k))
  111.         }
  112.  
  113.         if sum != count[k] {
  114.             panic(fmt.Sprintf("Sum for index %s is invalid", k))
  115.         }
  116.     }
  117. }
  118.  
  119. func describeFile(file) {
  120.    
  121. }
  122.  
  123. func checkFiles(user string, group string, files []string) {
  124.     gID, gErr := user.LookupGroup(group)
  125.     if gErr != nil {
  126.         panic(fmt.Sprintf("Group %s does not exist", group))
  127.     }
  128.  
  129.     uId, uErr := user.Lookup(user)
  130.     if uErr != nil {
  131.         panic(fmt.Sprintf("User %s does not exist", user))
  132.     }
  133.  
  134.  
  135.     info, err := os.Stat("/etc/xattr.conf")
  136.  
  137.     if err != nil {
  138.         panic(fmt.Sprintf("Cannot read %s", "test"))
  139.     }
  140.     _ = err1
  141.     fmt.Println(g.Gid)
  142.  
  143.     var UID int
  144.     var GID int
  145.     if stat, ok := info.Sys().(*syscall.Stat_t); ok {
  146.         UID = int(stat.Uid)
  147.         GID = int(stat.Gid)
  148.        
  149.         fmt.Println(UID)
  150.         fmt.Println(GID)
  151.     }
  152. }
  153.  
  154. func main() {
  155.     var configPath string
  156.    
  157.     flag.StringVar(&configPath, "config", "/etc/sphinx-health-check.yml", "Path to configuration file")
  158.     flag.Parse()
  159.  
  160.     config := parseConfig(configPath)
  161.     var db *sql.DB = dbConnection(&config.Parameters)
  162.     defer db.Close()
  163.  
  164.     // checkIndexes(db, &config.Indexes)
  165.     checkFiles()
  166.  
  167.     fmt.Println("Sphinx is working")
  168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top