func durInternalRater(cd *engine.CallDescriptor) (time.Duration, error) { ratingDb, err := engine.ConfigureRatingStorage(*ratingdb_type, *ratingdb_host, *ratingdb_port, *ratingdb_name, *ratingdb_user, *ratingdb_pass, *dbdata_encoding, *cacheDumpDir, *loadHistorySize) if err != nil { return nilDuration, fmt.Errorf("Could not connect to rating database: %s", err.Error()) } defer ratingDb.Close() engine.SetRatingStorage(ratingDb) accountDb, err := engine.ConfigureAccountingStorage(*accountdb_type, *accountdb_host, *accountdb_port, *accountdb_name, *accountdb_user, *accountdb_pass, *dbdata_encoding, *cacheDumpDir, *loadHistorySize) if err != nil { return nilDuration, fmt.Errorf("Could not connect to accounting database: %s", err.Error()) } defer accountDb.Close() engine.SetAccountingStorage(accountDb) if err := ratingDb.CacheRatingAll("cgr-tester"); err != nil { return nilDuration, fmt.Errorf("Cache rating error: %s", err.Error()) } if err := accountDb.CacheAccountingAll("cgr-tester"); err != nil { return nilDuration, fmt.Errorf("Cache accounting error: %s", err.Error()) } log.Printf("Runnning %d cycles...", *runs) var result *engine.CallCost j := 0 start := time.Now() for i := 0; i < *runs; i++ { result, err = cd.GetCost() if *memprofile != "" { runtime.MemProfileRate = 1 runtime.GC() f, err := os.Create(*memprofile) if err != nil { log.Fatal(err) } pprof.WriteHeapProfile(f) f.Close() break } j = i } log.Print(result, j, err) memstats := new(runtime.MemStats) runtime.ReadMemStats(memstats) log.Printf("memstats before GC: Kbytes = %d footprint = %d", memstats.HeapAlloc/1024, memstats.Sys/1024) return time.Since(start), nil }
func TestTutLclResetDataDb(t *testing.T) { if !*testLocal { return } ratingDb, err := engine.ConfigureRatingStorage(tutCfg.RatingDBType, tutCfg.RatingDBHost, tutCfg.RatingDBPort, tutCfg.RatingDBName, tutCfg.RatingDBUser, tutCfg.RatingDBPass, tutCfg.DBDataEncoding) if err != nil { t.Fatal(err) } accountDb, err := engine.ConfigureAccountingStorage(tutCfg.AccountDBType, tutCfg.AccountDBHost, tutCfg.AccountDBPort, tutCfg.AccountDBName, tutCfg.AccountDBUser, tutCfg.AccountDBPass, tutCfg.DBDataEncoding) if err != nil { t.Fatal(err) } for _, db := range []engine.Storage{ratingDb, accountDb} { if err := db.Flush(); err != nil { t.Fatal(err) } } }
func TestCDRStatsLclInitDataDb(t *testing.T) { if !*testLocal { return } ratingDb, err := engine.ConfigureRatingStorage(cdrstCfg.RatingDBType, cdrstCfg.RatingDBHost, cdrstCfg.RatingDBPort, cdrstCfg.RatingDBName, cdrstCfg.RatingDBUser, cdrstCfg.RatingDBPass, cdrstCfg.DBDataEncoding) if err != nil { t.Fatal("Cannot connect to dataDb", err) } accountDb, err := engine.ConfigureAccountingStorage(cdrstCfg.AccountDBType, cdrstCfg.AccountDBHost, cdrstCfg.AccountDBPort, cdrstCfg.AccountDBName, cdrstCfg.AccountDBUser, cdrstCfg.AccountDBPass, cdrstCfg.DBDataEncoding) if err != nil { t.Fatal("Cannot connect to dataDb", err) } for _, db := range []engine.Storage{ratingDb, accountDb} { if err := db.Flush(); err != nil { t.Fatal("Cannot reset dataDb", err) } } }
func main() { flag.Parse() if *version { fmt.Println("CGRateS " + utils.VERSION) return } var errRatingDb, errAccDb, errStorDb, err error var ratingDb engine.RatingStorage var accountDb engine.AccountingStorage var storDb engine.LoadStorage var rater, cdrstats, users *rpc.Client var loader engine.LoadReader if *migrateRC8 != "" { var db_nb int db_nb, err = strconv.Atoi(*datadb_name) if err != nil { log.Print("Redis db name must be an integer!") return } host := *datadb_host if *datadb_port != "" { host += ":" + *datadb_port } migratorRC8acc, err := NewMigratorRC8(host, db_nb, *datadb_pass, *dbdata_encoding) if err != nil { log.Print(err.Error()) return } if strings.Contains(*migrateRC8, "acc") || strings.Contains(*migrateRC8, "*all") { if err := migratorRC8acc.migrateAccounts(); err != nil { log.Print(err.Error()) } } db_nb, err = strconv.Atoi(*tpdb_name) if err != nil { log.Print("Redis db name must be an integer!") return } host = *tpdb_host if *tpdb_port != "" { host += ":" + *tpdb_port } migratorRC8rat, err := NewMigratorRC8(host, db_nb, *tpdb_pass, *dbdata_encoding) if err != nil { log.Print(err.Error()) return } if strings.Contains(*migrateRC8, "atr") || strings.Contains(*migrateRC8, "*all") { if err := migratorRC8rat.migrateActionTriggers(); err != nil { log.Print(err.Error()) } } if strings.Contains(*migrateRC8, "act") || strings.Contains(*migrateRC8, "*all") { if err := migratorRC8rat.migrateActions(); err != nil { log.Print(err.Error()) } } if strings.Contains(*migrateRC8, "dcs") || strings.Contains(*migrateRC8, "*all") { if err := migratorRC8rat.migrateDerivedChargers(); err != nil { log.Print(err.Error()) } } if strings.Contains(*migrateRC8, "apl") || strings.Contains(*migrateRC8, "*all") { if err := migratorRC8rat.migrateActionPlans(); err != nil { log.Print(err.Error()) } } if strings.Contains(*migrateRC8, "shg") || strings.Contains(*migrateRC8, "*all") { if err := migratorRC8rat.migrateSharedGroups(); err != nil { log.Print(err.Error()) } } log.Print("Done!") return } // Init necessary db connections, only if not already if !*dryRun { // make sure we do not need db connections on dry run, also not importing into any stordb if *fromStorDb { ratingDb, errRatingDb = engine.ConfigureRatingStorage(*tpdb_type, *tpdb_host, *tpdb_port, *tpdb_name, *tpdb_user, *tpdb_pass, *dbdata_encoding) accountDb, errAccDb = engine.ConfigureAccountingStorage(*datadb_type, *datadb_host, *datadb_port, *datadb_name, *datadb_user, *datadb_pass, *dbdata_encoding) storDb, errStorDb = engine.ConfigureLoadStorage(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass, *dbdata_encoding, cgrConfig.StorDBMaxOpenConns, cgrConfig.StorDBMaxIdleConns, cgrConfig.StorDBCDRSIndexes) } else if *toStorDb { // Import from csv files to storDb storDb, errStorDb = engine.ConfigureLoadStorage(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass, *dbdata_encoding, cgrConfig.StorDBMaxOpenConns, cgrConfig.StorDBMaxIdleConns, cgrConfig.StorDBCDRSIndexes) } else { // Default load from csv files to dataDb ratingDb, errRatingDb = engine.ConfigureRatingStorage(*tpdb_type, *tpdb_host, *tpdb_port, *tpdb_name, *tpdb_user, *tpdb_pass, *dbdata_encoding) accountDb, errAccDb = engine.ConfigureAccountingStorage(*datadb_type, *datadb_host, *datadb_port, *datadb_name, *datadb_user, *datadb_pass, *dbdata_encoding) } // Defer databases opened to be closed when we are done for _, db := range []engine.Storage{ratingDb, accountDb, storDb} { if db != nil { defer db.Close() } } // Stop on db errors for _, err = range []error{errRatingDb, errAccDb, errStorDb} { if err != nil { log.Fatalf("Could not open database connection: %v", err) } } if *toStorDb { // Import files from a directory into storDb if *tpid == "" { log.Fatal("TPid required, please define it via *-tpid* command argument.") } csvImporter := engine.TPCSVImporter{ TPid: *tpid, StorDb: storDb, DirPath: *dataPath, Sep: ',', Verbose: *verbose, ImportId: *runId, } if errImport := csvImporter.Run(); errImport != nil { log.Fatal(errImport) } return } } if *fromStorDb { // Load Tariff Plan from storDb into dataDb loader = storDb } else { // Default load from csv files to dataDb /*for fn, v := range engine.FileValidators { err := engine.ValidateCSVData(path.Join(*dataPath, fn), v.Rule) if err != nil { log.Fatal(err, "\n\t", v.Message) } }*/ loader = engine.NewFileCSVStorage(',', path.Join(*dataPath, utils.DESTINATIONS_CSV), path.Join(*dataPath, utils.TIMINGS_CSV), path.Join(*dataPath, utils.RATES_CSV), path.Join(*dataPath, utils.DESTINATION_RATES_CSV), path.Join(*dataPath, utils.RATING_PLANS_CSV), path.Join(*dataPath, utils.RATING_PROFILES_CSV), path.Join(*dataPath, utils.SHARED_GROUPS_CSV), path.Join(*dataPath, utils.LCRS_CSV), path.Join(*dataPath, utils.ACTIONS_CSV), path.Join(*dataPath, utils.ACTION_PLANS_CSV), path.Join(*dataPath, utils.ACTION_TRIGGERS_CSV), path.Join(*dataPath, utils.ACCOUNT_ACTIONS_CSV), path.Join(*dataPath, utils.DERIVED_CHARGERS_CSV), path.Join(*dataPath, utils.CDR_STATS_CSV), path.Join(*dataPath, utils.USERS_CSV), path.Join(*dataPath, utils.ALIASES_CSV), ) } tpReader := engine.NewTpReader(ratingDb, accountDb, loader, *tpid, *timezone, *loadHistorySize) err = tpReader.LoadAll() if err != nil { log.Fatal(err) } if *stats { tpReader.ShowStatistics() } if *validate { if !tpReader.IsValid() { return } } if *dryRun { // We were just asked to parse the data, not saving it return } if *historyServer != "" { // Init scribeAgent so we can store the differences if scribeAgent, err := history.NewProxyScribe(*historyServer, 3, 3); err != nil { log.Fatalf("Could not connect to history server, error: %s. Make sure you have properly configured it via -history_server flag.", err.Error()) return } else { engine.SetHistoryScribe(scribeAgent) //defer scribeAgent.Client.Close() } } else { log.Print("WARNING: Rates history archiving is disabled!") } if *raterAddress != "" { // Init connection to rater so we can reload it's data rater, err = rpc.Dial("tcp", *raterAddress) if err != nil { log.Fatalf("Could not connect to rater: %s", err.Error()) return } } else { log.Print("WARNING: Rates automatic cache reloading is disabled!") } if *cdrstatsAddress != "" { // Init connection to rater so we can reload it's data if *cdrstatsAddress == *raterAddress { cdrstats = rater } else { cdrstats, err = rpc.Dial("tcp", *cdrstatsAddress) if err != nil { log.Fatalf("Could not connect to CDRStats API: %s", err.Error()) return } } } else { log.Print("WARNING: CDRStats automatic data reload is disabled!") } if *usersAddress != "" { // Init connection to rater so we can reload it's data if *usersAddress == *raterAddress { users = rater } else { users, err = rpc.Dial("tcp", *usersAddress) if err != nil { log.Fatalf("Could not connect to Users API: %s", err.Error()) return } } } else { log.Print("WARNING: Users automatic data reload is disabled!") } // write maps to database if err := tpReader.WriteToDatabase(*flush, *verbose); err != nil { log.Fatal("Could not write to database: ", err) } if len(*historyServer) != 0 && *verbose { log.Print("Wrote history.") } var dstIds, rplIds, rpfIds, actIds, shgIds, alsIds, lcrIds, dcsIds []string if rater != nil { dstIds, _ = tpReader.GetLoadedIds(utils.DESTINATION_PREFIX) rplIds, _ = tpReader.GetLoadedIds(utils.RATING_PLAN_PREFIX) rpfIds, _ = tpReader.GetLoadedIds(utils.RATING_PROFILE_PREFIX) actIds, _ = tpReader.GetLoadedIds(utils.ACTION_PREFIX) shgIds, _ = tpReader.GetLoadedIds(utils.SHARED_GROUP_PREFIX) alsIds, _ = tpReader.GetLoadedIds(utils.ALIASES_PREFIX) lcrIds, _ = tpReader.GetLoadedIds(utils.LCR_PREFIX) dcsIds, _ = tpReader.GetLoadedIds(utils.DERIVEDCHARGERS_PREFIX) } actTmgIds, _ := tpReader.GetLoadedIds(utils.ACTION_PLAN_PREFIX) var statsQueueIds []string if cdrstats != nil { statsQueueIds, _ = tpReader.GetLoadedIds(utils.CDR_STATS_PREFIX) } var userIds []string if users != nil { userIds, _ = tpReader.GetLoadedIds(utils.USERS_PREFIX) } // release the reader with it's structures tpReader.Init() // Reload scheduler and cache if rater != nil { reply := "" // Reload cache first since actions could be calling info from within if *verbose { log.Print("Reloading cache") } if *flush { dstIds, rplIds, rpfIds, lcrIds = nil, nil, nil, nil // Should reload all these on flush } if err = rater.Call("ApierV1.ReloadCache", utils.ApiReloadCache{ DestinationIds: dstIds, RatingPlanIds: rplIds, RatingProfileIds: rpfIds, ActionIds: actIds, SharedGroupIds: shgIds, Aliases: alsIds, LCRIds: lcrIds, DerivedChargers: dcsIds, }, &reply); err != nil { log.Printf("WARNING: Got error on cache reload: %s\n", err.Error()) } if len(actTmgIds) != 0 { if *verbose { log.Print("Reloading scheduler") } if err = rater.Call("ApierV1.ReloadScheduler", "", &reply); err != nil { log.Printf("WARNING: Got error on scheduler reload: %s\n", err.Error()) } } } if cdrstats != nil { if *flush { statsQueueIds = []string{} // Force reload all } if len(statsQueueIds) != 0 { if *verbose { log.Print("Reloading CDRStats data") } var reply string if err := cdrstats.Call("CDRStatsV1.ReloadQueues", utils.AttrCDRStatsReloadQueues{StatsQueueIds: statsQueueIds}, &reply); err != nil { log.Printf("WARNING: Failed reloading stat queues, error: %s\n", err.Error()) } } } if users != nil { if len(userIds) > 0 { if *verbose { log.Print("Reloading Users data") } var reply string if err := cdrstats.Call("UsersV1.ReloadUsers", "", &reply); err != nil { log.Printf("WARNING: Failed reloading users data, error: %s\n", err.Error()) } } } }
func main() { flag.Parse() if *version { fmt.Println("CGRateS " + utils.VERSION) return } if *pidFile != "" { writePid() } if *singlecpu { runtime.GOMAXPROCS(1) // Having multiple cpus slows down computing due to CPU management, to be reviewed in future Go releases } if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } cfg, err = config.NewCGRConfigFromFolder(*cfgDir) if err != nil { utils.Logger.Crit(fmt.Sprintf("Could not parse config: %s exiting!", err)) return } config.SetCgrConfig(cfg) // Share the config object if *raterEnabled { cfg.RaterEnabled = *raterEnabled } if *schedEnabled { cfg.SchedulerEnabled = *schedEnabled } if *cdrsEnabled { cfg.CDRSEnabled = *cdrsEnabled } var ratingDb engine.RatingStorage var accountDb engine.AccountingStorage var logDb engine.LogStorage var loadDb engine.LoadStorage var cdrDb engine.CdrStorage if cfg.RaterEnabled || cfg.SchedulerEnabled { // Only connect to dataDb if necessary ratingDb, err = engine.ConfigureRatingStorage(cfg.TpDbType, cfg.TpDbHost, cfg.TpDbPort, cfg.TpDbName, cfg.TpDbUser, cfg.TpDbPass, cfg.DBDataEncoding) if err != nil { // Cannot configure getter database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure dataDb: %s exiting!", err)) return } defer ratingDb.Close() engine.SetRatingStorage(ratingDb) accountDb, err = engine.ConfigureAccountingStorage(cfg.DataDbType, cfg.DataDbHost, cfg.DataDbPort, cfg.DataDbName, cfg.DataDbUser, cfg.DataDbPass, cfg.DBDataEncoding) if err != nil { // Cannot configure getter database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure dataDb: %s exiting!", err)) return } defer accountDb.Close() engine.SetAccountingStorage(accountDb) } if cfg.RaterEnabled || cfg.CDRSEnabled || cfg.SchedulerEnabled { // Only connect to storDb if necessary logDb, err = engine.ConfigureLogStorage(cfg.StorDBType, cfg.StorDBHost, cfg.StorDBPort, cfg.StorDBName, cfg.StorDBUser, cfg.StorDBPass, cfg.DBDataEncoding, cfg.StorDBMaxOpenConns, cfg.StorDBMaxIdleConns) if err != nil { // Cannot configure logger database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure logger database: %s exiting!", err)) return } defer logDb.Close() engine.SetStorageLogger(logDb) // loadDb,cdrDb and logDb are all mapped on the same stordb storage loadDb = logDb.(engine.LoadStorage) cdrDb = logDb.(engine.CdrStorage) engine.SetCdrStorage(cdrDb) } engine.SetRoundingDecimals(cfg.RoundingDecimals) stopHandled := false // Rpc/http server server := new(engine.Server) // Async starts here, will follow cgrates.json start order exitChan := make(chan bool) // Define internal connections via channels internalBalancerChan := make(chan *balancer2go.Balancer, 1) internalRaterChan := make(chan *engine.Responder, 1) internalSchedulerChan := make(chan *scheduler.Scheduler, 1) internalCdrSChan := make(chan *engine.CdrServer, 1) internalCdrStatSChan := make(chan engine.StatsInterface, 1) internalHistorySChan := make(chan history.Scribe, 1) internalPubSubSChan := make(chan engine.PublisherSubscriber, 1) internalUserSChan := make(chan engine.UserService, 1) internalAliaseSChan := make(chan engine.AliasService, 1) // Start balancer service if cfg.BalancerEnabled { go startBalancer(internalBalancerChan, &stopHandled, exitChan) // Not really needed async here but to cope with uniformity } // Start rater service if cfg.RaterEnabled { go startRater(internalRaterChan, internalBalancerChan, internalSchedulerChan, internalCdrStatSChan, internalHistorySChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan, server, ratingDb, accountDb, loadDb, cdrDb, logDb, &stopHandled, exitChan) } // Start Scheduler if cfg.SchedulerEnabled { go startScheduler(internalSchedulerChan, ratingDb, exitChan) } // Start CDR Server if cfg.CDRSEnabled { go startCDRS(internalCdrSChan, logDb, cdrDb, internalRaterChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan, internalCdrStatSChan, server, exitChan) } // Start CDR Stats server if cfg.CDRStatsEnabled { go startCdrStats(internalCdrStatSChan, ratingDb, accountDb, server) } // Start CDRC components if necessary go startCdrcs(internalCdrSChan, internalRaterChan, exitChan) // Start SM-FreeSWITCH if cfg.SmFsConfig.Enabled { go startSmFreeSWITCH(internalRaterChan, cdrDb, exitChan) // close all sessions on shutdown go shutdownSessionmanagerSingnalHandler(exitChan) } // Start SM-Kamailio if cfg.SmKamConfig.Enabled { go startSmKamailio(internalRaterChan, cdrDb, exitChan) } // Start SM-OpenSIPS if cfg.SmOsipsConfig.Enabled { go startSmOpenSIPS(internalRaterChan, cdrDb, exitChan) } // Register session manager service // FixMe: make sure this is thread safe if cfg.SmFsConfig.Enabled || cfg.SmKamConfig.Enabled || cfg.SmOsipsConfig.Enabled { // Register SessionManagerV1 service smRpc = new(v1.SessionManagerV1) server.RpcRegister(smRpc) } // Start HistoryS service if cfg.HistoryServerEnabled { go startHistoryServer(internalHistorySChan, server, exitChan) } // Start PubSubS service if cfg.PubSubServerEnabled { go startPubSubServer(internalPubSubSChan, accountDb, server) } // Start Aliases service if cfg.AliasesServerEnabled { go startAliasesServer(internalAliaseSChan, accountDb, server, exitChan) } // Start users service if cfg.UserServerEnabled { go startUsersServer(internalUserSChan, accountDb, server, exitChan) } // Serve rpc connections go startRpc(server, internalRaterChan, internalCdrSChan, internalCdrStatSChan, internalHistorySChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan) <-exitChan if *pidFile != "" { if err := os.Remove(*pidFile); err != nil { utils.Logger.Warning("Could not remove pid file: " + err.Error()) } } utils.Logger.Info("Stopped all components. CGRateS shutdown!") }
func main() { flag.Parse() if *version { fmt.Println("CGRateS " + utils.VERSION) return } var errRatingDb, errAccDb, errStorDb, err error var ratingDb engine.RatingStorage var accountDb engine.AccountingStorage var storDb engine.LoadStorage var rater, cdrstats *rpc.Client var loader engine.TPLoader // Init necessary db connections, only if not already if !*dryRun { // make sure we do not need db connections on dry run, also not importing into any stordb if *fromStorDb { ratingDb, errRatingDb = engine.ConfigureRatingStorage(*ratingdb_type, *ratingdb_host, *ratingdb_port, *ratingdb_name, *ratingdb_user, *ratingdb_pass, *dbdata_encoding) accountDb, errAccDb = engine.ConfigureAccountingStorage(*accountdb_type, *accountdb_host, *accountdb_port, *accountdb_name, *accountdb_user, *accountdb_pass, *dbdata_encoding) storDb, errStorDb = engine.ConfigureLoadStorage(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass, *dbdata_encoding) } else if *toStorDb { // Import from csv files to storDb storDb, errStorDb = engine.ConfigureLoadStorage(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass, *dbdata_encoding) } else { // Default load from csv files to dataDb ratingDb, errRatingDb = engine.ConfigureRatingStorage(*ratingdb_type, *ratingdb_host, *ratingdb_port, *ratingdb_name, *ratingdb_user, *ratingdb_pass, *dbdata_encoding) accountDb, errAccDb = engine.ConfigureAccountingStorage(*accountdb_type, *accountdb_host, *accountdb_port, *accountdb_name, *accountdb_user, *accountdb_pass, *dbdata_encoding) } // Defer databases opened to be closed when we are done for _, db := range []engine.Storage{ratingDb, accountDb, storDb} { if db != nil { defer db.Close() } } // Stop on db errors for _, err = range []error{errRatingDb, errAccDb, errStorDb} { if err != nil { log.Fatalf("Could not open database connection: %v", err) } } if *toStorDb { // Import files from a directory into storDb if *tpid == "" { log.Fatal("TPid required, please define it via *-tpid* command argument.") } csvImporter := engine.TPCSVImporter{*tpid, storDb, *dataPath, ',', *verbose, *runId} if errImport := csvImporter.Run(); errImport != nil { log.Fatal(errImport) } return } } if *fromStorDb { // Load Tariff Plan from storDb into dataDb loader = engine.NewDbReader(storDb, ratingDb, accountDb, *tpid) } else { // Default load from csv files to dataDb for fn, v := range engine.FileValidators { err := engine.ValidateCSVData(path.Join(*dataPath, fn), v.Rule) if err != nil { log.Fatal(err, "\n\t", v.Message) } } loader = engine.NewFileCSVReader(ratingDb, accountDb, ',', path.Join(*dataPath, utils.DESTINATIONS_CSV), path.Join(*dataPath, utils.TIMINGS_CSV), path.Join(*dataPath, utils.RATES_CSV), path.Join(*dataPath, utils.DESTINATION_RATES_CSV), path.Join(*dataPath, utils.RATING_PLANS_CSV), path.Join(*dataPath, utils.RATING_PROFILES_CSV), path.Join(*dataPath, utils.SHARED_GROUPS_CSV), path.Join(*dataPath, utils.LCRS_CSV), path.Join(*dataPath, utils.ACTIONS_CSV), path.Join(*dataPath, utils.ACTION_PLANS_CSV), path.Join(*dataPath, utils.ACTION_TRIGGERS_CSV), path.Join(*dataPath, utils.ACCOUNT_ACTIONS_CSV), path.Join(*dataPath, utils.DERIVED_CHARGERS_CSV), path.Join(*dataPath, utils.CDR_STATS_CSV)) } err = loader.LoadAll() if err != nil { log.Fatal(err) } if *stats { loader.ShowStatistics() } if *dryRun { // We were just asked to parse the data, not saving it return } if *historyServer != "" { // Init scribeAgent so we can store the differences if scribeAgent, err := history.NewProxyScribe(*historyServer); err != nil { log.Fatalf("Could not connect to history server, error: %s. Make sure you have properly configured it via -history_server flag.", err.Error()) return } else { engine.SetHistoryScribe(scribeAgent) defer scribeAgent.Client.Close() } } else { log.Print("WARNING: Rates history archiving is disabled!") } if *raterAddress != "" { // Init connection to rater so we can reload it's data rater, err = rpc.Dial("tcp", *raterAddress) if err != nil { log.Fatalf("Could not connect to rater: %s", err.Error()) return } } else { log.Print("WARNING: Rates automatic cache reloading is disabled!") } if *cdrstatsAddress != "" { // Init connection to rater so we can reload it's data if *cdrstatsAddress == *raterAddress { cdrstats = rater } else { cdrstats, err = rpc.Dial("tcp", *cdrstatsAddress) if err != nil { log.Fatalf("Could not connect to CDRStats API: %s", err.Error()) return } } } else { log.Print("WARNING: CDRStats automatic data reload is disabled!") } // write maps to database if err := loader.WriteToDatabase(*flush, *verbose); err != nil { log.Fatal("Could not write to database: ", err) } if len(*historyServer) != 0 && *verbose { log.Print("Wrote history.") } // Reload scheduler and cache if rater != nil { reply := "" dstIds, _ := loader.GetLoadedIds(engine.DESTINATION_PREFIX) rplIds, _ := loader.GetLoadedIds(engine.RATING_PLAN_PREFIX) rpfIds, _ := loader.GetLoadedIds(engine.RATING_PROFILE_PREFIX) actIds, _ := loader.GetLoadedIds(engine.ACTION_PREFIX) shgIds, _ := loader.GetLoadedIds(engine.SHARED_GROUP_PREFIX) rpAliases, _ := loader.GetLoadedIds(engine.RP_ALIAS_PREFIX) accAliases, _ := loader.GetLoadedIds(engine.ACC_ALIAS_PREFIX) lcrIds, _ := loader.GetLoadedIds(engine.LCR_PREFIX) dcs, _ := loader.GetLoadedIds(engine.DERIVEDCHARGERS_PREFIX) // Reload cache first since actions could be calling info from within if *verbose { log.Print("Reloading cache") } if *flush { dstIds, rplIds, rpfIds, rpAliases, lcrIds = nil, nil, nil, nil, nil // Should reload all these on flush } if err = rater.Call("ApierV1.ReloadCache", utils.ApiReloadCache{dstIds, rplIds, rpfIds, actIds, shgIds, rpAliases, accAliases, lcrIds, dcs}, &reply); err != nil { log.Printf("WARNING: Got error on cache reload: %s\n", err.Error()) } actTmgIds, _ := loader.GetLoadedIds(engine.ACTION_TIMING_PREFIX) if len(actTmgIds) != 0 { if *verbose { log.Print("Reloading scheduler") } if err = rater.Call("ApierV1.ReloadScheduler", "", &reply); err != nil { log.Printf("WARNING: Got error on scheduler reload: %s\n", err.Error()) } } } if cdrstats != nil { statsQueueIds, _ := loader.GetLoadedIds(engine.CDR_STATS_PREFIX) if *flush { statsQueueIds = []string{} // Force reload all } if len(statsQueueIds) != 0 { if *verbose { log.Print("Reloading CDRStats data") } var reply string if err := cdrstats.Call("CDRStatsV1.ReloadQueues", utils.AttrCDRStatsReloadQueues{StatsQueueIds: statsQueueIds}, &reply); err != nil { log.Printf("WARNING: Failed reloading stat queues, error: %s\n", err.Error()) } } } }
func main() { flag.Parse() if *version { fmt.Println("CGRateS " + utils.VERSION) return } if *pidFile != "" { writePid() } if *singlecpu { runtime.GOMAXPROCS(1) // Having multiple cpus may slow down computing due to CPU management, to be reviewed in future Go releases } exitChan := make(chan bool) if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } if *scheduledShutdown != "" { shutdownDur, err := utils.ParseDurationWithSecs(*scheduledShutdown) if err != nil { log.Fatal(err) } go func() { // Schedule shutdown time.Sleep(shutdownDur) exitChan <- true }() } cfg, err = config.NewCGRConfigFromFolder(*cfgDir) if err != nil { log.Fatalf("Could not parse config: ", err) return } lgLevel := cfg.LogLevel if *logLevel != -1 { // Modify the log level if provided by command arguments lgLevel = *logLevel } utils.Logger.SetLogLevel(lgLevel) config.SetCgrConfig(cfg) // Share the config object cache.NewCache(cfg.CacheConfig) var ratingDb engine.RatingStorage var accountDb engine.AccountingStorage var loadDb engine.LoadStorage var cdrDb engine.CdrStorage if cfg.RALsEnabled || cfg.SchedulerEnabled || cfg.CDRStatsEnabled { // Only connect to dataDb if necessary ratingDb, err = engine.ConfigureRatingStorage(cfg.TpDbType, cfg.TpDbHost, cfg.TpDbPort, cfg.TpDbName, cfg.TpDbUser, cfg.TpDbPass, cfg.DBDataEncoding, cfg.CacheConfig, cfg.LoadHistorySize) if err != nil { // Cannot configure getter database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure dataDb: %s exiting!", err)) return } defer ratingDb.Close() engine.SetRatingStorage(ratingDb) } if cfg.RALsEnabled || cfg.CDRStatsEnabled || cfg.PubSubServerEnabled || cfg.AliasesServerEnabled || cfg.UserServerEnabled { accountDb, err = engine.ConfigureAccountingStorage(cfg.DataDbType, cfg.DataDbHost, cfg.DataDbPort, cfg.DataDbName, cfg.DataDbUser, cfg.DataDbPass, cfg.DBDataEncoding, cfg.CacheConfig, cfg.LoadHistorySize) if err != nil { // Cannot configure getter database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure dataDb: %s exiting!", err)) return } defer accountDb.Close() engine.SetAccountingStorage(accountDb) if err := engine.CheckVersion(nil); err != nil { fmt.Println(err.Error()) return } } if cfg.RALsEnabled || cfg.CDRSEnabled || cfg.SchedulerEnabled { // Only connect to storDb if necessary storDb, err := engine.ConfigureStorStorage(cfg.StorDBType, cfg.StorDBHost, cfg.StorDBPort, cfg.StorDBName, cfg.StorDBUser, cfg.StorDBPass, cfg.DBDataEncoding, cfg.StorDBMaxOpenConns, cfg.StorDBMaxIdleConns, cfg.StorDBCDRSIndexes) if err != nil { // Cannot configure logger database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure logger database: %s exiting!", err)) return } defer storDb.Close() // loadDb,cdrDb and storDb are all mapped on the same stordb storage loadDb = storDb.(engine.LoadStorage) cdrDb = storDb.(engine.CdrStorage) engine.SetCdrStorage(cdrDb) } engine.SetRoundingDecimals(cfg.RoundingDecimals) engine.SetRpSubjectPrefixMatching(cfg.RpSubjectPrefixMatching) engine.SetLcrSubjectPrefixMatching(cfg.LcrSubjectPrefixMatching) stopHandled := false // Rpc/http server server := new(utils.Server) // Async starts here, will follow cgrates.json start order // Define internal connections via channels internalBalancerChan := make(chan *balancer2go.Balancer, 1) internalRaterChan := make(chan rpcclient.RpcClientConnection, 1) cacheDoneChan := make(chan struct{}, 1) internalSchedulerChan := make(chan *scheduler.Scheduler, 1) internalCdrSChan := make(chan rpcclient.RpcClientConnection, 1) internalCdrStatSChan := make(chan rpcclient.RpcClientConnection, 1) internalHistorySChan := make(chan rpcclient.RpcClientConnection, 1) internalPubSubSChan := make(chan rpcclient.RpcClientConnection, 1) internalUserSChan := make(chan rpcclient.RpcClientConnection, 1) internalAliaseSChan := make(chan rpcclient.RpcClientConnection, 1) internalSMGChan := make(chan *sessionmanager.SMGeneric, 1) internalRLSChan := make(chan rpcclient.RpcClientConnection, 1) // Start balancer service if cfg.BalancerEnabled { go startBalancer(internalBalancerChan, &stopHandled, exitChan) // Not really needed async here but to cope with uniformity } // Start rater service if cfg.RALsEnabled { go startRater(internalRaterChan, cacheDoneChan, internalBalancerChan, internalSchedulerChan, internalCdrStatSChan, internalHistorySChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan, server, ratingDb, accountDb, loadDb, cdrDb, &stopHandled, exitChan) } // Start Scheduler if cfg.SchedulerEnabled { go startScheduler(internalSchedulerChan, cacheDoneChan, ratingDb, exitChan) } // Start CDR Server if cfg.CDRSEnabled { go startCDRS(internalCdrSChan, cdrDb, accountDb, internalRaterChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan, internalCdrStatSChan, server, exitChan) } // Start CDR Stats server if cfg.CDRStatsEnabled { go startCdrStats(internalCdrStatSChan, ratingDb, accountDb, server) } // Start CDRC components if necessary go startCdrcs(internalCdrSChan, internalRaterChan, exitChan) // Start SM-Generic if cfg.SmGenericConfig.Enabled { go startSmGeneric(internalSMGChan, internalRaterChan, internalCdrSChan, server, exitChan) } // Start SM-FreeSWITCH if cfg.SmFsConfig.Enabled { go startSmFreeSWITCH(internalRaterChan, internalCdrSChan, internalRLSChan, cdrDb, exitChan) // close all sessions on shutdown go shutdownSessionmanagerSingnalHandler(exitChan) } // Start SM-Kamailio if cfg.SmKamConfig.Enabled { go startSmKamailio(internalRaterChan, internalCdrSChan, cdrDb, exitChan) } // Start SM-OpenSIPS if cfg.SmOsipsConfig.Enabled { go startSmOpenSIPS(internalRaterChan, internalCdrSChan, cdrDb, exitChan) } // Register session manager service // FixMe: make sure this is thread safe if cfg.SmGenericConfig.Enabled || cfg.SmFsConfig.Enabled || cfg.SmKamConfig.Enabled || cfg.SmOsipsConfig.Enabled || cfg.SMAsteriskCfg().Enabled { // Register SessionManagerV1 service smRpc = new(v1.SessionManagerV1) server.RpcRegister(smRpc) } if cfg.SMAsteriskCfg().Enabled { go startSMAsterisk(internalSMGChan, exitChan) } if cfg.DiameterAgentCfg().Enabled { go startDiameterAgent(internalSMGChan, internalPubSubSChan, exitChan) } // Start HistoryS service if cfg.HistoryServerEnabled { go startHistoryServer(internalHistorySChan, server, exitChan) } // Start PubSubS service if cfg.PubSubServerEnabled { go startPubSubServer(internalPubSubSChan, accountDb, server) } // Start Aliases service if cfg.AliasesServerEnabled { go startAliasesServer(internalAliaseSChan, accountDb, server, exitChan) } // Start users service if cfg.UserServerEnabled { go startUsersServer(internalUserSChan, accountDb, server, exitChan) } // Start RL service if cfg.ResourceLimiterCfg().Enabled { go startResourceLimiterService(internalRLSChan, internalCdrStatSChan, cfg, accountDb, server, exitChan) } // Serve rpc connections go startRpc(server, internalRaterChan, internalCdrSChan, internalCdrStatSChan, internalHistorySChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan, internalSMGChan) <-exitChan if *pidFile != "" { if err := os.Remove(*pidFile); err != nil { utils.Logger.Warning("Could not remove pid file: " + err.Error()) } } utils.Logger.Info("Stopped all components. CGRateS shutdown!") }
func main() { flag.Parse() if *version { fmt.Println("CGRateS " + utils.VERSION) return } if *pidFile != "" { writePid() } // runtime.GOMAXPROCS(runtime.NumCPU()) // For now it slows down computing due to CPU management, to be reviewed in future Go releases cfg, err = config.NewCGRConfigFromFile(cfgPath) if err != nil { engine.Logger.Crit(fmt.Sprintf("Could not parse config: %s exiting!", err)) return } config.SetCgrConfig(cfg) // Share the config object if *raterEnabled { cfg.RaterEnabled = *raterEnabled } if *schedEnabled { cfg.SchedulerEnabled = *schedEnabled } if *cdrsEnabled { cfg.CDRSEnabled = *cdrsEnabled } if *cdrcEnabled { cfg.CdrcEnabled = *cdrcEnabled } if *mediatorEnabled { cfg.MediatorEnabled = *mediatorEnabled } // some consitency checks errCfg := checkConfigSanity() if errCfg != nil { engine.Logger.Crit(errCfg.Error()) return } var ratingDb engine.RatingStorage var accountDb engine.AccountingStorage var logDb engine.LogStorage var loadDb engine.LoadStorage var cdrDb engine.CdrStorage ratingDb, err = engine.ConfigureRatingStorage(cfg.RatingDBType, cfg.RatingDBHost, cfg.RatingDBPort, cfg.RatingDBName, cfg.RatingDBUser, cfg.RatingDBPass, cfg.DBDataEncoding) if err != nil { // Cannot configure getter database, show stopper engine.Logger.Crit(fmt.Sprintf("Could not configure dataDb: %s exiting!", err)) return } defer ratingDb.Close() engine.SetRatingStorage(ratingDb) accountDb, err = engine.ConfigureAccountingStorage(cfg.AccountDBType, cfg.AccountDBHost, cfg.AccountDBPort, cfg.AccountDBName, cfg.AccountDBUser, cfg.AccountDBPass, cfg.DBDataEncoding) if err != nil { // Cannot configure getter database, show stopper engine.Logger.Crit(fmt.Sprintf("Could not configure dataDb: %s exiting!", err)) return } defer accountDb.Close() engine.SetAccountingStorage(accountDb) if cfg.StorDBType == SAME { logDb = ratingDb.(engine.LogStorage) } else { logDb, err = engine.ConfigureLogStorage(cfg.StorDBType, cfg.StorDBHost, cfg.StorDBPort, cfg.StorDBName, cfg.StorDBUser, cfg.StorDBPass, cfg.DBDataEncoding) if err != nil { // Cannot configure logger database, show stopper engine.Logger.Crit(fmt.Sprintf("Could not configure logger database: %s exiting!", err)) return } } defer logDb.Close() engine.SetStorageLogger(logDb) // loadDb,cdrDb and logDb are all mapped on the same stordb storage loadDb = logDb.(engine.LoadStorage) cdrDb = logDb.(engine.CdrStorage) engine.SetRoundingDecimals(cfg.RoundingDecimals) if cfg.SMDebitInterval > 0 { if dp, err := time.ParseDuration(fmt.Sprintf("%vs", cfg.SMDebitInterval)); err == nil { engine.SetDebitPeriod(dp) } } stopHandled := false // Async starts here rpcWait := make([]chan struct{}, 0) // Rpc server will start as soon as this list is consumed httpWait := make([]chan struct{}, 0) // Http server will start as soon as this list is consumed var cacheChan chan struct{} if cfg.RaterEnabled { // Cache rating if rater enabled cacheChan = make(chan struct{}) rpcWait = append(rpcWait, cacheChan) go cacheData(ratingDb, accountDb, cacheChan) } if cfg.RaterEnabled && cfg.RaterBalancer != "" && !cfg.BalancerEnabled { go registerToBalancer() go stopRaterSignalHandler() stopHandled = true } if cfg.CDRStatsEnabled { // Init it here so we make it availabe to the Apier cdrStats = engine.NewStats(ratingDb) if cfg.CDRStatConfig != nil && len(cfg.CDRStatConfig.Metrics) != 0 { cdrStats.AddQueue(engine.NewCdrStatsFromCdrStatsCfg(cfg.CDRStatConfig), nil) } server.RpcRegister(cdrStats) server.RpcRegister(&apier.CDRStatsV1{cdrStats}) // Public APIs } responder := &engine.Responder{ExitChan: exitChan} apierRpc := &apier.ApierV1{StorDb: loadDb, RatingDb: ratingDb, AccountDb: accountDb, CdrDb: cdrDb, LogDb: logDb, Config: cfg, Responder: responder, CdrStatsSrv: cdrStats} if cfg.RaterEnabled && !cfg.BalancerEnabled && cfg.RaterBalancer != utils.INTERNAL { engine.Logger.Info("Registering Rater service") server.RpcRegister(responder) server.RpcRegister(apierRpc) } if cfg.BalancerEnabled { engine.Logger.Info("Registering Balancer service.") go stopBalancerSignalHandler() stopHandled = true responder.Bal = bal server.RpcRegister(responder) server.RpcRegister(apierRpc) if cfg.RaterEnabled { engine.Logger.Info("<Balancer> Registering internal rater") bal.AddClient("local", new(engine.ResponderWorker)) } } if !stopHandled { go generalSignalHandler() } if cfg.SchedulerEnabled { engine.Logger.Info("Starting CGRateS Scheduler.") go func() { sched := scheduler.NewScheduler() go reloadSchedulerSingnalHandler(sched, accountDb) apierRpc.Sched = sched sched.LoadActionTimings(accountDb) sched.Loop() }() } var histServChan chan struct{} // Will be initialized only if the server starts if cfg.HistoryServerEnabled { histServChan = make(chan struct{}) rpcWait = append(rpcWait, histServChan) go startHistoryServer(histServChan) } if cfg.HistoryAgentEnabled { engine.Logger.Info("Starting CGRateS History Agent.") go startHistoryAgent(histServChan) } var medChan chan struct{} if cfg.MediatorEnabled { engine.Logger.Info("Starting CGRateS Mediator service.") medChan = make(chan struct{}) go startMediator(responder, logDb, cdrDb, cacheChan, medChan) } var cdrsChan chan struct{} if cfg.CDRSEnabled { engine.Logger.Info("Starting CGRateS CDRS service.") cdrsChan = make(chan struct{}) httpWait = append(httpWait, cdrsChan) go startCDRS(responder, cdrDb, medChan, cdrsChan) } if cfg.SMEnabled { engine.Logger.Info("Starting CGRateS SessionManager service.") go startSessionManager(responder, logDb, cacheChan) // close all sessions on shutdown go shutdownSessionmanagerSingnalHandler() } var cdrcEnabled bool if cfg.CdrcEnabled { // Start default cdrc configured in csv here cdrcEnabled = true go startCdrc(cdrsChan, cfg.CdrcCdrs, cfg.CdrcCdrType, cfg.CdrcCdrInDir, cfg.CdrcCdrOutDir, cfg.CdrcSourceId, cfg.CdrcRunDelay, cfg.CdrcCsvSep, cfg.CdrcCdrFields) } if cfg.XmlCfgDocument != nil { for _, xmlCdrc := range cfg.XmlCfgDocument.GetCdrcCfgs("") { if !xmlCdrc.Enabled { continue } cdrcEnabled = true go startCdrc(cdrsChan, xmlCdrc.CdrsAddress, xmlCdrc.CdrType, xmlCdrc.CdrInDir, xmlCdrc.CdrOutDir, xmlCdrc.CdrSourceId, time.Duration(xmlCdrc.RunDelay), xmlCdrc.CsvSeparator, xmlCdrc.CdrRSRFields()) } } if cdrcEnabled { engine.Logger.Info("Starting CGRateS CDR client.") } // Start the servers go serveRpc(rpcWait) go serveHttp(httpWait) <-exitChan if *pidFile != "" { if err := os.Remove(*pidFile); err != nil { engine.Logger.Warning("Could not remove pid file: " + err.Error()) } } engine.Logger.Info("Stopped all components. CGRateS shutdown!") }