github.com/orofarne/hammy@v0.0.0-20130409105742-374fadfd6ecb/src/hammy/config.go (about)

     1  package hammy
     2  
     3  import "fmt"
     4  
     5  // Programm configuration
     6  type Config struct {
     7  	// Http interface for incoming data
     8  	IncomingHttp struct {
     9  		// Addr for incomming-data
    10  		// e.g. "0.0.0.0:4000" or ":4000" for ipv6
    11  		Addr string
    12  	}
    13  	// Http interface for data requests
    14  	DataHttp struct {
    15  		// Addr for data request
    16  		// e.g. "0.0.0.0:4000" or ":4000" for ipv6
    17  		Addr string
    18  		// Http prefix
    19  		// e.g. /data (default)
    20  		Prefix string
    21  	}
    22  	// Logging options
    23  	Log struct {
    24  		// Files for logging (stderr if empty)
    25  		// For hammyd daemon
    26  		HammyDFile string
    27  		// For hammydatad daemon
    28  		HammyDataDFile string
    29  		// FIXME CmdOutput
    30  		CmdOutputFile string
    31  	}
    32  	// Debug and statistics
    33  	Debug struct {
    34  		// Addrs for debug and statistic information
    35  		// e.g. "localhost:6060" (default)
    36  		// For hammyd daemon
    37  		HammyDAddr string
    38  		// For hammydatad daemon
    39  		HammyDataDAddr string
    40  	}
    41  	// Workers
    42  	Workers struct {
    43  		// Count of workers
    44  		PoolSize uint
    45  		// Worker cmd
    46  		CmdLine string
    47  		// Worker live limit
    48  		MaxIter uint
    49  		// Worker timeout (before kill)
    50  		Timeout uint
    51  	}
    52  	// Send buffer
    53  	SendBuffer struct {
    54  		SleepTime float32
    55  	}
    56  	// Coucbase for triggers configuration
    57  	CouchbaseTriggers struct {
    58  		// Use this implementation
    59  		Active bool
    60  		// e.g. "http://dev-couchbase.example.com:8091/"
    61  		ConnectTo string
    62  		// e.g. "default"
    63  		Pool string
    64  		// e.g. "default"
    65  		Bucket string
    66  	}
    67  	// Coucbase for state storage
    68  	CouchbaseStates struct {
    69  		// Use this implementation
    70  		Active bool
    71  		// e.g. "http://dev-couchbase.example.com:8091/"
    72  		ConnectTo string
    73  		// e.g. "default"
    74  		Pool string
    75  		// e.g. "default"
    76  		Bucket string
    77  		// TTL in seconds, default 86400 (day)
    78  		Ttl int
    79  	}
    80  	// Data saver
    81  	CouchbaseSaver struct {
    82  		// Use this implementation
    83  		Active bool
    84  		// e.g. "http://dev-couchbase.example.com:8091/"
    85  		ConnectTo string
    86  		// e.g. "default"
    87  		Pool string
    88  		// e.g. "default"
    89  		Bucket string
    90  		// Internal write queue size
    91  		QueueSize uint
    92  		// Connections for saving
    93  		SavePoolSize uint
    94  		// TTL in seconds, default 259200 (3 days)
    95  		Ttl int
    96  	}
    97  	// Data reader
    98  	CouchbaseDataReader struct {
    99  		// Use this implementation
   100  		Active bool
   101  		// e.g. "http://dev-couchbase.example.com:8091/"
   102  		ConnectTo string
   103  		// e.g. "default"
   104  		Pool string
   105  		// e.g. "default"
   106  		Bucket string
   107  	}
   108  	// MySQL for triggers configuration
   109  	MySQLTriggers struct {
   110  		// Use this implementation
   111  		Active bool
   112  		// Database to connect
   113  		Database string
   114  		// DB user
   115  		User string
   116  		// DB user password
   117  		Password string
   118  		// table that contains triggers (host, trigger)
   119  		Table string
   120  		// Limit for parallel connections
   121  		MaxConn int
   122  	}
   123  	// MySQL for states
   124  	MySQLStates struct {
   125  		// Use this implementation
   126  		Active bool
   127  		// Database to connect
   128  		Database string
   129  		// DB user
   130  		User string
   131  		// DB user password
   132  		Password string
   133  		// table that contains states (host, state, cas)
   134  		Table string
   135  		// Limit for parallel connections
   136  		MaxConn int
   137  	}
   138  	// MySQL historical data saver
   139  	MySQLSaver struct {
   140  		// Use this implementation
   141  		Active bool
   142  		// Database to connect
   143  		Database string
   144  		// DB user
   145  		User string
   146  		// DB user password
   147  		Password string
   148  		// table that contains numeric history
   149  		Table string
   150  		// table that contains text history
   151  		LogTable string
   152  		// table that contains hosts
   153  		HostTable string
   154  		// table that contains items
   155  		ItemTable string
   156  		// Limit for parallel connections
   157  		MaxConn int
   158  	}
   159  	// MySQL historical data reader
   160  	MySQLDataReader struct {
   161  		// Use this implementation
   162  		Active bool
   163  		// Database to connect
   164  		Database string
   165  		// DB user
   166  		User string
   167  		// DB user password
   168  		Password string
   169  		// table that contains numeric history
   170  		Table string
   171  		// table that contains text history
   172  		LogTable string
   173  		// table that contains hosts
   174  		HostTable string
   175  		// table that contains items
   176  		ItemTable string
   177  		// Limit for parallel connections
   178  		MaxConn int
   179  	}
   180  }
   181  
   182  // Setup defaults for empty values in configs
   183  // Returns an error if mandatory field omited
   184  func SetConfigDefaults(cfg *Config) error {
   185  	// Section [IncomingHttp]
   186  	if cfg.IncomingHttp.Addr == "" { cfg.IncomingHttp.Addr = ":4000" }
   187  
   188  	// Section [DataHttp]
   189  	if cfg.DataHttp.Addr == "" { cfg.DataHttp.Addr = ":4001" }
   190  	if cfg.DataHttp.Prefix == "" { cfg.DataHttp.Prefix = "/data" }
   191  
   192  	// Section [Log]
   193  
   194  	// Section [Debug]
   195  	if cfg.Debug.HammyDAddr == "" { cfg.Debug.HammyDAddr = "localhost:6060" }
   196  	if cfg.Debug.HammyDataDAddr == "" { cfg.Debug.HammyDataDAddr = "localhost:6061" }
   197  
   198  	// Section [SendBuffer]
   199  	if cfg.SendBuffer.SleepTime == 0.0 { cfg.SendBuffer.SleepTime = 10.0 }
   200  
   201  	// Section [Workers]
   202  	if cfg.Workers.PoolSize == 0 { cfg.Workers.PoolSize = 5 }
   203  	if cfg.Workers.CmdLine == "" { return fmt.Errorf("Empty cfg.Workers.CmdLine") }
   204  	if cfg.Workers.MaxIter == 0 { cfg.Workers.MaxIter = 1000 }
   205  	if cfg.Workers.Timeout == 0 { cfg.Workers.Timeout = 1 }
   206  
   207  	// Section [CouchbaseTriggers]
   208  	if cfg.CouchbaseTriggers.Active {
   209  		if cfg.CouchbaseTriggers.ConnectTo == "" { return fmt.Errorf("Empty cfg.CouchbaseTriggers.ConnectTo") }
   210  		if cfg.CouchbaseTriggers.Pool == "" { cfg.CouchbaseTriggers.Pool = "default" }
   211  		if cfg.CouchbaseTriggers.Bucket == "" { return fmt.Errorf("Empty cfg.CouchbaseTriggers.Bucket") }
   212  	}
   213  
   214  	// Section [CouchbaseStates]
   215  	if cfg.CouchbaseStates.Active {
   216  		if cfg.CouchbaseStates.ConnectTo == "" { return fmt.Errorf("Empty cfg.CouchbaseStates.ConnectTo") }
   217  		if cfg.CouchbaseStates.Pool == "" { cfg.CouchbaseStates.Pool = "default" }
   218  		if cfg.CouchbaseStates.Bucket == "" { return fmt.Errorf("Empty cfg.CouchbaseStates.Bucket") }
   219  		if cfg.CouchbaseStates.Ttl == 0 { cfg.CouchbaseStates.Ttl = 86400 }
   220  	}
   221  
   222  	// Section [CouchbaseSaver]
   223  	if cfg.CouchbaseSaver.Active {
   224  		if cfg.CouchbaseSaver.ConnectTo == "" { return fmt.Errorf("Empty cfg.CouchbaseSaver.ConnectTo") }
   225  		if cfg.CouchbaseSaver.Pool == "" { cfg.CouchbaseSaver.Pool = "default" }
   226  		if cfg.CouchbaseSaver.Bucket == "" { return fmt.Errorf("Empty cfg.CouchbaseSaver.Bucket") }
   227  		if cfg.CouchbaseSaver.QueueSize == 0 { cfg.CouchbaseSaver.QueueSize = 10000 }
   228  		if cfg.CouchbaseSaver.SavePoolSize == 0 { cfg.CouchbaseSaver.SavePoolSize = 10 }
   229  		if cfg.CouchbaseSaver.Ttl == 0 { cfg.CouchbaseSaver.Ttl = 259200 }
   230  	}
   231  
   232  	// Section [CouchbaseDataReader]
   233  	if cfg.CouchbaseDataReader.Active {
   234  		if cfg.CouchbaseDataReader.ConnectTo == "" { return fmt.Errorf("Empty cfg.CouchbaseDataReader.ConnectTo") }
   235  		if cfg.CouchbaseDataReader.Pool == "" { cfg.CouchbaseDataReader.Pool = "default" }
   236  		if cfg.CouchbaseDataReader.Bucket == "" { return fmt.Errorf("Empty cfg.CouchbaseDataReader.Bucket") }
   237  	}
   238  
   239  	// Section [MySQLTriggers]
   240  	if cfg.MySQLTriggers.Active {
   241  		if cfg.MySQLTriggers.Database == "" { return fmt.Errorf("Empty cfg.MySQLTriggers.Database") }
   242  		if cfg.MySQLTriggers.User == "" { return fmt.Errorf("Empty cfg.MySQLTriggers.User") }
   243  		if cfg.MySQLTriggers.Table == "" { return fmt.Errorf("Empty cfg.MySQLTriggers.Table") }
   244  		if cfg.MySQLTriggers.MaxConn == 0 { cfg.MySQLTriggers.MaxConn = 10 }
   245  	}
   246  
   247  	// Section [MySQLStates]
   248  	if cfg.MySQLStates.Active {
   249  		if cfg.MySQLStates.Database == "" { return fmt.Errorf("Empty cfg.MySQLStates.Database") }
   250  		if cfg.MySQLStates.User == "" { return fmt.Errorf("Empty cfg.MySQLStates.User") }
   251  		if cfg.MySQLStates.Table == "" { return fmt.Errorf("Empty cfg.MySQLStates.Table") }
   252  		if cfg.MySQLStates.MaxConn == 0 { cfg.MySQLStates.MaxConn = 10 }
   253  	}
   254  
   255  	// Section [MySQLSaver]
   256  	if cfg.MySQLSaver.Active {
   257  		if cfg.MySQLSaver.Database == "" { return fmt.Errorf("Empty cfg.MySQLSaver.Database") }
   258  		if cfg.MySQLSaver.User == "" { return fmt.Errorf("Empty cfg.MySQLSaver.User") }
   259  		if cfg.MySQLSaver.Table == "" { return fmt.Errorf("Empty cfg.MySQLSaver.Table") }
   260  		if cfg.MySQLSaver.LogTable == "" { return fmt.Errorf("Empty cfg.MySQLSaver.LogTable") }
   261  		if cfg.MySQLSaver.HostTable == "" { return fmt.Errorf("Empty cfg.MySQLSaver.HostTable") }
   262  		if cfg.MySQLSaver.ItemTable == "" { return fmt.Errorf("Empty cfg.MySQLSaver.ItemTable") }
   263  		if cfg.MySQLSaver.MaxConn == 0 { cfg.MySQLSaver.MaxConn = 10 }
   264  	}
   265  
   266  	// Section [MySQLDataReader]
   267  	if cfg.MySQLDataReader.Active {
   268  		if cfg.MySQLDataReader.Database == "" { return fmt.Errorf("Empty cfg.MySQLDataReader.Database") }
   269  		if cfg.MySQLDataReader.User == "" { return fmt.Errorf("Empty cfg.MySQLDataReader.User") }
   270  		if cfg.MySQLDataReader.Table == "" { return fmt.Errorf("Empty cfg.MySQLDataReader.Table") }
   271  		if cfg.MySQLDataReader.LogTable == "" { return fmt.Errorf("Empty cfg.MySQLDataReader.LogTable") }
   272  		if cfg.MySQLDataReader.HostTable == "" { return fmt.Errorf("Empty cfg.MySQLDataReader.HostTable") }
   273  		if cfg.MySQLDataReader.ItemTable == "" { return fmt.Errorf("Empty cfg.MySQLDataReader.ItemTable") }
   274  		if cfg.MySQLDataReader.MaxConn == 0 { cfg.MySQLDataReader.MaxConn = 10 }
   275  	}
   276  
   277  	// Counts
   278  	// 1) TriggersGetter
   279  	{
   280  		k := 0
   281  
   282  		if cfg.CouchbaseTriggers.Active { k++ }
   283  		if cfg.MySQLTriggers.Active { k++ }
   284  
   285  		if k != 1 {
   286  			return fmt.Errorf("Invalid count of active TriggersGetter drivers: %d", k)
   287  		}
   288  	}
   289  	// 2) StateKeeper
   290  	{
   291  		k := 0
   292  
   293  		if cfg.CouchbaseStates.Active { k++ }
   294  		if cfg.MySQLStates.Active { k++ }
   295  
   296  		if k != 1 {
   297  			return fmt.Errorf("Invalid count of active StateKeeper drivers: %d", k)
   298  		}
   299  	}
   300  	// 3) DataSaver
   301  	{
   302  		k := 0
   303  
   304  		if cfg.CouchbaseSaver.Active { k++ }
   305  		if cfg.MySQLSaver.Active { k++ }
   306  
   307  		if k != 1 {
   308  			return fmt.Errorf("Invalid count of active DataSaver drivers: %d", k)
   309  		}
   310  	}
   311  	// 4) DataReader
   312  	{
   313  		k := 0
   314  
   315  		if cfg.CouchbaseDataReader.Active { k++ }
   316  		if cfg.MySQLDataReader.Active { k++ }
   317  
   318  		if k != 1 {
   319  			return fmt.Errorf("Invalid count of active DataReader drivers: %d", k)
   320  		}
   321  	}
   322  
   323  	return nil
   324  }