-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
563 lines (495 loc) · 13.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
log "github.com/cihub/seelog"
"github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/native"
"io/ioutil"
"labix.org/v2/mgo"
"os"
"os/signal"
"redis"
"sync"
"syscall"
"time"
)
const (
VERSION = "X.X.X"
)
type Context struct {
Name string
Direction int
}
type Config struct {
DbMySqlHost string
DbMySqlUser string
DbMySqlPassword string
DbMySqlName string
DbMySqlFetchRowNumber string
MongoHost string
MongoDbName string
EventsMongoHost string
AsteriskID string
AsteriskAddr string
AsteriskPort int
AsteriskUser string
AsteriskPassword string
TestCallActive bool
PurgeCelEvents bool
TestCallSchedule int
DialplanContext []Context
Notifications []string
Dids []string
ExcludeFromAnalytics []string
CleanupRequests []string
}
var (
config *Config
configLock = new(sync.RWMutex)
timeZoneOffset int64
isImportProcessing bool
configFile = flag.String("config", "config.json", "Configuration file path")
importTick = flag.Int("tick", 10, "Importing tick cycle")
version = flag.Bool("version", false, "show version")
eventWatcher *EventWatcher
testCallOriginator *callOriginator
stopImportJob chan bool
stopGenerateTestCall chan bool
)
const (
DIRECTION_CALL_OUT = 1
DIRECTION_CALL_IN = 2
DIRECTION_CALL_IGNORE = 3
)
var (
DISPOSITION_TRANSLATION map[int]int = map[int]int{
0: 0,
1: 16, // ANSWER
2: 17, // BUSY
3: 19, // NOANSWER
4: 21, // CANCEL
5: 34, // CONGESTION
6: 47, // CHANUNAVAIL
7: 0, // DONTCALL
8: 0, // TORTURE
9: 0, // INVALIDARGS
10: 41, // FAILED
}
)
var (
DIC_DISPOSITION map[string]int = map[string]int{
"ANSWER": 1,
"ANSWERED": 1,
"BUSY": 2,
"NOANSWER": 3,
"NO ANSWER": 3,
"CANCEL": 4,
"CONGESTION": 5,
"CHANUNAVAIL": 6,
"DONTCALL": 7,
"TORTURE": 8,
"INVALIDARGS": 9,
"FAIL": 10,
"FAILED": 10,
}
)
/**
*
*/
func loadConfig(fail bool) {
file, err := ioutil.ReadFile(*configFile)
if err != nil {
log.Errorf("Can't open configuration file : %s", err)
if fail {
os.Exit(1)
}
}
temp := new(Config)
if err = json.Unmarshal(file, temp); err != nil {
log.Error("Can't load json configuration : %s", err)
if fail {
os.Exit(1)
}
}
configLock.Lock()
config = temp
if len(config.MongoDbName) == 0 {
config.MongoDbName = "revor"
}
configLock.Unlock()
}
func loadLogger() {
logger, err := log.LoggerFromConfigAsFile("logger.xml")
if err != nil {
log.Error("Can not load the logger configuration file, Please check if the file logger.xml exists on current directory", err)
os.Exit(1)
} else {
log.ReplaceLogger(logger)
logger.Flush()
}
}
func GetConfig() *Config {
configLock.RLock()
defer configLock.RUnlock()
return config
}
func init() {
//
}
func getInOutStatus(cdr RawCall) (status int, err error) {
return getInOutContextStatus(cdr.Dcontext)
}
func getInOutContextStatus(context string) (status int, err error) {
for i := range config.DialplanContext {
if config.DialplanContext[i].Name == context {
status = config.DialplanContext[i].Direction
return status, nil
}
}
log.Infof("Can not find the call direction for the context [%s].", context)
return status, errors.New("Can't find the context direction for the context : " + context)
}
func syncPublish(spec *redis.ConnectionSpec, channel string, messageType string) {
client, err := redis.NewSynchClientWithSpec(spec)
if err != nil {
log.Errorf("Failed to create the redis client : %s", err)
os.Exit(1)
}
msg := []byte(fmt.Sprintf("{id : %s }", messageType))
rcvCnt, err := client.Publish(channel, msg)
if err != nil {
log.Errorf("Error to publish the messge to the redis : %s", err)
} else {
log.Debugf("Message published to %d subscribers", rcvCnt)
}
client.Quit()
}
func sendEventNotification(flag int, datas string) {
ev := &Event{
Mask: new(BitSet),
Datas: datas,
}
ev.Mask.Set(flag)
eventWatcher.event <- ev
}
func sendMySqlEventNotification(flag int) {
datas := fmt.Sprintf("MySql server : %s change state", config.DbMySqlHost)
sendEventNotification(flag, datas)
}
func sendMongoEventNotification(flag int) {
datas := fmt.Sprintf("Mongo server : %s change state", config.DbMySqlHost)
sendEventNotification(flag, datas)
}
func importJob() {
//
db := mysql.New("tcp", "", config.DbMySqlHost, config.DbMySqlUser, config.DbMySqlPassword, config.DbMySqlName)
log.Debugf("Connecting to the database %s %s %s %s.", config.DbMySqlHost, config.DbMySqlUser, config.DbMySqlPassword, config.DbMySqlName)
//
err := db.Connect()
if err != nil {
sendMySqlEventNotification(MYSQKO)
log.Criticalf("Can't connect to the mysql database error : %s.", err)
return
}
sendMySqlEventNotification(MYSQOK)
log.Debug("Connected to the mysql database with success.")
//
session, err := mgo.Dial(config.MongoHost)
if err != nil {
log.Debugf("Can't connect to the mongo database error : %s.", err)
sendMongoEventNotification(MONGKO)
return
}
session.SetMode(mgo.Monotonic, true)
defer session.Close()
sendMongoEventNotification(MONGOK)
log.Debug("Connected to the mongo database with success.")
//
err = executeCustomRequests(db)
if err != nil {
log.Errorf("Error to execute customise request : %s", err)
log.Flush()
os.Exit(1)
}
//
cdrs, err := getMysqlCdr(db)
//
if err != nil {
log.Criticalf("Can not get records from mysql cause error [%s].", err)
log.Flush()
os.Exit(1)
}
log.Tracef("Start records parcing.")
//
var incommingCount = 0
var outgoingCount = 0
for _, cdr := range cdrs {
cdr.AsteriskId = config.AsteriskID
var datetime = cdr.Calldate.Format(time.RFC3339)
log.Tracef("Get raw cdr for the date [%s], the clid [%s] and the context [%s] from asterisk [%s]", datetime, cdr.ClidNumber, cdr.Dcontext, cdr.AsteriskId)
//var cel Cel
//cel, err = getMySqlCel(db, cdr.Uniqueid)
var inoutstatus, err = getInOutStatus(cdr)
if err != nil {
log.Criticalf("Get error[%s]. Please check your configuration file.", err)
log.Flush()
os.Exit(1)
}
if inoutstatus == 1 {
outgoingCount++
} else if inoutstatus == 2 {
incommingCount++
}
cdr.InoutStatus = inoutstatus
var dispostionCode = DIC_DISPOSITION[cdr.DispositionStr]
if dispostionCode > 0 {
cdr.Disposition = DISPOSITION_TRANSLATION[dispostionCode]
} else {
cdr.Disposition = 0
}
//if cel.EventTime > 0 {
// //extract the timezone offset
// cdr.AnswerWaitTime = int(cel.EventTime - cdr.Calldate.Unix() - timeZoneOffset)
//}
//
callDetails, err := getMySqlCallDetails(db, cdr.Uniqueid)
if err != nil {
log.Criticalf("Try to get the call details but get the error[%s].", err)
log.Flush()
os.Exit(1)
}
//
log.Tracef("Get [%d] details records for the call with uniqueid [%s].", len(callDetails), cdr.Uniqueid)
if callDetails != nil {
cdr.CallDetails = callDetails
}
//
if cdr.InoutStatus == DIRECTION_CALL_IN {
var did = ""
var peer = ""
var exten = ""
if len(cdr.CallDetails) == 0 && cdr.Dnid != "" {
//a workaround if I can get cel records
did = cdr.Dnid
log.Tracef("Force did from cdr dnid [%s].", did)
}
for i := range cdr.CallDetails {
var callDetail = cdr.CallDetails[i]
if i == 0 && callDetail.EventType == "CHAN_START" {
exten = callDetail.Exten
did = callDetail.Exten
}
if callDetail.EventType == "ANSWER" && exten == callDetail.CidDnid && did == "" {
//try to find did
did = callDetail.CidDnid
} else if callDetail.EventType == "BRIDGE_START" {
//bridge start gives the time before answer
cdr.AnswerWaitTime = int(callDetail.EventTime.Unix() - cdr.Calldate.Unix())
peer = getPeerFromChannel(callDetail.Peer)
} else if callDetail.EventType == "BRIDGE_END" && peer == "" {
//idea to find the last BRIDGE_END event and get the extention from it
peer = getPeerFromChannel(callDetail.Peer)
break
}
}
if peer == "" {
peer = getPeerFromChannel(cdr.Dstchannel)
}
if peer == "" {
//can be case that the call is not answered
peer = cdr.Dst
}
cdr.Peer = peer
//there is a possibility to have for incomming call a peer for the did number
//this is dependes of the customer configuration
//try to find a did value from database defined by the customer
err := isDid(session, did)
if err == nil {
cdr.Did = did
} else {
cdr.Did = ""
}
log.Tracef("Did [%s] and peer [%s] for the call with uniqueid [%s].\n", did, peer, cdr.Uniqueid)
if cdr.Dst == "s" && peer != "" {
//
cdr.Dst = cdr.Peer
}
} //end of the incomming call process
//
//
err = importCdrToMongo(session, cdr)
var importedStatus = 1
if err != nil {
log.Errorf("Can't import cdr to mongo [%v].", err)
log.Flush()
os.Exit(1)
}
//
log.Debugf("Import executed for unique id [%s] with code : [%d], try process the mysql updating.\n",
cdr.Uniqueid, importedStatus)
err = udpateMySqlCdrImportStatus(db, cdr.Uniqueid, 1)
if err != nil {
log.Errorf("Can't update the import status for the call with unique id [%s].", cdr.Uniqueid)
log.Flush()
os.Exit(1)
}
err = deleteMySqlCelRecord(db, cdr.Uniqueid)
if err != nil {
log.Errorf("Can't delete cel records for uniqueid[%s].", cdr.Uniqueid)
}
}
//
log.Trace("End of cdr parsing.\n")
//
spec := redis.DefaultSpec()
channel := "channel_cdr"
//
if incommingCount > 0 {
syncPublish(spec, channel, "cdrincomming")
}
if outgoingCount > 0 {
syncPublish(spec, channel, "cdroutgoing")
}
//
}
func cleanup() {
stopImportJob <- true
if config.TestCallActive == true {
//wait testing call process
stopGenerateTestCall <- true
}
//
sendEventNotification(APPSTO, "Application stopped")
//wait for the eventWatcher
select {
case <-eventWatcher.done:
log.Info("Event watcher stopped.")
}
log.Info("Execute the application cleanup")
log.Flush()
}
func generateTestCall() {
//
testCallOriginator.testCall <- true
//
select {
case res := <-testCallOriginator.resultTestCall:
if res != nil {
data := fmt.Sprintf("Connection failed to the asterisk server %s.", res)
sendEventNotification(TCALKO, data)
return
} else {
sendEventNotification(TCALOK, "Test call ok")
}
}
//little stupid wait
time.Sleep(3 * time.Second)
db := mysql.New("tcp", "", config.DbMySqlHost, config.DbMySqlUser, config.DbMySqlPassword, config.DbMySqlName)
log.Debugf("Connecting to the database %s %s %s %s.", config.DbMySqlHost, config.DbMySqlUser, config.DbMySqlPassword, config.DbMySqlName)
//
err := db.Connect()
if err != nil {
data := fmt.Sprintf("Failed generate test call : %v.", err)
sendEventNotification(CCALKO, data)
return
}
//
cdrs, err := getMysqlCdrTestCall(db)
if len(cdrs) == 0 {
data := fmt.Sprintf("Failed find a test call for the asterisk server %s.", config.AsteriskID)
sendEventNotification(CCALKO, data)
log.Errorf(data)
} else {
for _, cdr := range cdrs {
err = deleteMySqlCdrRecord(db, cdr.Uniqueid)
if err != nil {
log.Errorf("Can't delete the test call record with unique id [%s] cause get an error %v.", cdr.Uniqueid, err)
cleanup()
os.Exit(1)
}
}
data := fmt.Sprintf("Check call ok for the asterisk server %s.", config.AsteriskID)
sendEventNotification(CCALOK, data)
log.Infof("Asterisk the test call processed with success.")
}
}
func checkConfigAndDie() {
if len(config.AsteriskAddr) == 0 {
log.Critical("Asterisk address is null or empty. Please check the configuraiton file.\n")
log.Flush()
os.Exit(1)
}
if len(config.AsteriskUser) == 0 || len(config.AsteriskPassword) == 0 {
log.Critical("Asterisk credentials missing. Please check the configuration file.\n")
log.Flush()
os.Exit(1)
}
if config.TestCallActive && config.TestCallSchedule < 30 {
log.Criticalf("Asterisk tesing interval is too short : %d. Minimal value is 30(seconds).Please check the configuration file.\n", config.TestCallSchedule)
log.Flush()
os.Exit(1)
}
}
func main() {
flag.Parse()
//
if *version {
fmt.Printf("Version : %s\n", VERSION)
fmt.Println("Get fun! Live well !")
return
}
//
loadLogger()
loadConfig(true)
//
config = GetConfig()
//to be carrefule and check if the configuraiton is not
checkConfigAndDie()
//
eventWatcher = NewEventWatcher(config)
go eventWatcher.run()
//
if config.TestCallSchedule > 0 {
log.Infof("Create test call originator for the asterisk %s:%d", config.AsteriskAddr, config.AsteriskPort)
testCallOriginator = NewCallOriginator(config.AsteriskAddr, config.AsteriskPort, config.AsteriskUser, config.AsteriskPassword)
}
//something wrong I cannot trup SIGUSR1 :-)
/*s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGUSR1)*/
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
cleanup()
os.Exit(1)
/*<-s
loadConfig(false)
log.Info("Configuration reloading")*/
}()
//
log.Infof("Starting for %s", config.AsteriskID)
//dummy flag for indicate that the import is processing
isImportProcessing = false
//
now := time.Now()
_, timeZoneOffset := now.Zone()
log.Infof("Startring and using the timezone offset used : %d.", timeZoneOffset)
//go for schedule job
duration := time.Duration(*importTick) * time.Second
//ticker := time.NewTicker(duration)
stopImportJob = schedule(importJob, duration)
sendEventNotification(APPSTA, "Application vorimport started.")
durationTestCall := time.Duration(config.TestCallSchedule) * time.Second
//ticker := time.NewTicker(duration)
if config.TestCallActive {
stopGenerateTestCall = schedule(generateTestCall, durationTestCall)
}
for {
log.Debug("Working...")
time.Sleep(1000 * time.Second) //
}
}