forked from NuclearAPK/go-techLog1C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
827 lines (685 loc) · 22.8 KB
/
Copy pathmain.go
File metadata and controls
827 lines (685 loc) · 22.8 KB
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
package main
import (
"bytes"
"crypto/md5"
"crypto/tls"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"time"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/gomodule/redigo/redis"
logr "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
// =======================================================================================
type conf struct {
Path string `yaml:"path"`
RedisAddr string `yaml:"redis_addr"`
RedisLogin string `yaml:"redis_login"`
RedisPassword string `yaml:"redis_password"`
RedisDatabase int `yaml:"redis_database"`
ElasticAddr string `yaml:"elastic_addr"`
ElasticLogin string `yaml:"elastic_login"`
ElasticPassword string `yaml:"elastic_password"`
ElasticIndx string `yaml:"elastic_indx"`
ElasticMaxRetrires int `yaml:"elastic_maxretries"`
ElasticTimeout int `yaml:"elastic_timeout"`
ElasticTimeoutHeader int `yaml:"elastic_timeout_header"`
ElasticMaxContentLength int `yaml:"elastic_max_content_length"`
ElasticBulkSize int64 `yaml:"elastic_bulksize"`
TechLogDetailsEvents string `yaml:"tech_log_details_events"`
MaxDop int `yaml:"maxdop"`
Sorting int `yaml:"sorting"`
PathLogFile string `yaml:"path_logfile"`
LogLevel int `yaml:"log_level"`
LogLifeSpan int `yaml:"log_life_span"`
DeleteTabsInContexts bool `yaml:"delete_tabs_in_contexts"`
DeletePostfixInNameVirtualTables bool `yaml:"delete_postfix_in_name_virtual_tables"`
InsecureSkipVerify bool `yaml:"skip_verify_certificates"`
}
type bulkResponse struct {
Errors bool `json:"errors"`
Items []struct {
Index struct {
ID string `json:"_id"`
Result string `json:"result"`
Status int `json:"status"`
Error struct {
Type string `json:"type"`
Reason string `json:"reason"`
Cause struct {
Type string `json:"type"`
Reason string `json:"reason"`
} `json:"caused_by"`
} `json:"error"`
} `json:"index"`
} `json:"items"`
}
type files struct {
Path string
Size int64
LastPosition int64
FileDate string
DataCreate time.Time
ProcessNameID string
BlokingID string
}
func (c *conf) getConfig() *conf {
yamlFile, err := ioutil.ReadFile("./conf/settings.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return c
}
// получаем дату время в формате jdata
func getDateEvent(fileDate, word string) string {
year := fileDate[0:2]
month := fileDate[2:4]
day := fileDate[4:6]
minuts := fileDate[6:8]
dateEventArray := []string{"20", year, "-", month, "-", day, "T", minuts, ":", word}
buffer := bytes.Buffer{}
for _, val := range dateEventArray {
buffer.WriteString(val)
}
return buffer.String()
}
func track() time.Time {
return time.Now()
}
func duration(start time.Time) {
logr.WithFields(logr.Fields{
"object": "Reading tech log 1C",
"title": "Succeful reading",
}).Infof("Final time is: %v\n", time.Since(start))
}
func getFileParametersRedis(conn redis.Conn, idFile string) int64 {
valueTmpRedis, err := redis.String(conn.Do("GET", idFile))
if err != nil {
return 0
}
valueRedis, err := strconv.ParseInt(valueTmpRedis, 10, 64)
if err != nil {
return 0
}
return valueRedis
}
func setFileParametersRedis(conn redis.Conn, idFile string, pos int64) {
conn.Do("SET", idFile, strconv.FormatInt(pos, 10))
}
// удаление ключа в redis
func deleteFileParametersRedis(conn redis.Conn, idFile string) {
conn.Do("DEL", idFile)
}
func getMapEvent(str *string) map[string]string {
var (
Value string
Property string
)
strEvent := strings.Split(*str, ",")
paramets := make(map[string]string)
for idxStr, Event := range strEvent {
// поиск символа =
runeIndex := strings.Index(Event, "=")
if runeIndex > 0 {
PropertyTmp := strings.ToLower(Event[0:runeIndex])
Property = strings.Replace(PropertyTmp, ":", "_", 1)
ValueTmp := Event[runeIndex+1:]
Value = ValueTmp
} else {
Value = Event
switch idxStr {
case 0:
Property = "duration"
case 1:
Property = "event_techlog"
case 2:
Property = "level"
default:
Property = "unclassified"
}
}
paramets[Property] = Value
}
return paramets
}
// заменяет заданные символы в строках, подходящих под регулярные выражения на новые символы
func replaceGaps(str *string, exp string, old string, new string) {
regexGaps := regexp.MustCompile(exp)
gapsStrings := regexGaps.FindAllString(*str, -1)
for _, gapString := range gapsStrings {
rightStringTmp := strings.Replace(*str, gapString, strings.ReplaceAll(gapString, old, new), -1)
*str = rightStringTmp
}
}
func replaceSymbols(str *string, config *conf) {
if config.DeleteTabsInContexts {
*str = strings.ReplaceAll(*str, "\t", "")
}
if config.DeletePostfixInNameVirtualTables {
regex := regexp.MustCompile(`(?m)#tt[0-9]+`)
*str = regex.ReplaceAllString(*str, "#tt")
}
var re = regexp.MustCompile(`(?m)\r\n`)
i := re.FindStringIndex(*str)
if i != nil && len(i) == 2 && (i[0] < 2 || i[1] == len(*str)) {
*str = strings.Replace(*str, "\r\n", "", 1)
}
}
// Reverse возвращает инвентированную строку
func Reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
func isLetter(c rune) bool {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
}
func getFilesPacked(arrFiles []*files, maxdop int) map[int][]files {
take := make(map[int][]files)
lenFiles := len(arrFiles)
var j int = 0
for i := 0; i < lenFiles; i++ {
take[j] = append(take[j], *arrFiles[i])
j++
if j == maxdop {
j = 0
}
}
return take
}
func readFile(file files) ([]byte, int64, error) {
currPosition := file.LastPosition
openFile, err := os.Open(file.Path)
if err != nil {
return nil, 0, err
}
buffer := make([]byte, 64)
var data []byte
openFile.Seek(file.LastPosition, 0)
for {
n, err := openFile.Read(buffer)
currPosition += int64(n)
if err == io.EOF { // если конец файла
break // выходим из цикла
}
data = append(data, buffer[:n]...)
}
openFile.Close()
return data, currPosition, nil
}
// формирование наименование индекса по правилам, заданным в conf файле
func getIndexName(config *conf) string {
today := time.Now()
str := strings.ReplaceAll(config.ElasticIndx, "yyyy", strconv.Itoa(today.Year()))
str = strings.ReplaceAll(str, "MM", strconv.Itoa(int(today.Month())))
str = strings.ReplaceAll(str, "dd", strconv.Itoa(today.Day()))
str = strings.ReplaceAll(str, "hh", strconv.Itoa(today.Hour()))
str = strings.ReplaceAll(str, "mm", strconv.Itoa(today.Minute()))
str = strings.ReplaceAll(str, "ss", strconv.Itoa(today.Second()))
return str
}
// считываем карты индексов в соответствие
func getMappings() map[string]string {
mapping := make(map[string]string)
files, err := getFilesArray("./maps/")
if err != nil {
logr.WithFields(logr.Fields{
"object": "Maps files",
"title": "Failure to scan directory",
}).Error(err)
}
separator := string(os.PathSeparator)
for _, file := range files {
data, _, err := readFile(file)
if err != nil {
logr.WithFields(logr.Fields{
"object": "Map file",
"title": "Failure to read map file",
}).Error(err)
}
tmpKey := strings.Split(file.Path, separator)[1]
key := tmpKey[0 : len(tmpKey)-4]
mapping[key] = string(data)
}
return mapping
}
func createElasticsearchClient(config *conf) (*elasticsearch.Client, error) {
cfgElastic := elasticsearch.Config{
Addresses: []string{config.ElasticAddr},
Username: config.ElasticLogin,
Password: config.ElasticPassword,
RetryOnStatus: []int{502, 503, 504, 429},
RetryBackoff: func(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond },
MaxRetries: config.ElasticMaxRetrires,
EnableMetrics: true,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: time.Second * time.Duration(config.ElasticTimeout),
}).DialContext,
ResponseHeaderTimeout: time.Second * time.Duration(config.ElasticTimeoutHeader), // prevent hanging connections
TLSClientConfig: &tls.Config{
InsecureSkipVerify: config.InsecureSkipVerify,
},
},
}
es, err := elasticsearch.NewClient(cfgElastic)
return es, err
}
func jobExtractTechLogs(filesInPackage []files, keyInPackage int, config *conf, c chan int) {
var (
rightString string
indexName = getIndexName(config)
res *esapi.Response
raw map[string]interface{}
blk *bulkResponse
)
// 1. подключаемся к эластичному
es, _ := createElasticsearchClient(config)
// 2. подключаемся к redis
conn, _ := redis.Dial("tcp", config.RedisAddr,
redis.DialUsername(config.RedisLogin),
redis.DialPassword(config.RedisPassword),
redis.DialDatabase(config.RedisDatabase),
)
defer conn.Close()
// 3. считываем мэппинг для индексов elastic из map файлов
mapping := getMappings()
// 4. работаем с файлами
RegExpEvents := fmt.Sprintf("(%s)=", config.TechLogDetailsEvents)
re := regexp.MustCompile("[0-9][0-9]:[0-9][0-9].[0-9]+-")
reContextstrings := regexp.MustCompile(RegExpEvents)
for _, file := range filesInPackage {
data, currentPosition, err := readFile(file)
if err != nil {
deleteFileParametersRedis(conn, file.BlokingID)
logr.WithFields(logr.Fields{
"object": "Data",
"title": "Open file data",
}).Fatal(err)
}
if data == nil {
deleteFileParametersRedis(conn, file.BlokingID)
continue
}
headings := re.FindAllString(string(data), -1)
words := re.Split(string(data), -1)
if headings == nil {
deleteFileParametersRedis(conn, file.BlokingID)
continue
}
var mapEventsBuffer = map[string]*bytes.Buffer{}
mapIndicies := make(map[string]string)
// 0.9. разбор строк, разделенных регулярным выражением по времени событий
// пробегаемся по частям строк с заголовками
var IndexPostfix int = 0
for idx, word := range headings {
word := strings.TrimRight(word, "-")
dataEvent := getDateEvent(file.FileDate, word)
var multilineMap = make(map[string]string)
if reContextstrings.MatchString(words[idx+1]) {
lenWords := len(words[idx+1])
garbageStrings := reContextstrings.Split(words[idx+1], -1)
var tmpLen int = 0
for i := len(garbageStrings) - 1; i > 0; i-- {
garbageString := strings.TrimRight(garbageStrings[i], ",")
var sb strings.Builder
var lenSb int = 0
lenGarbageString := len(garbageStrings[i])
for j := (lenWords - lenGarbageString - tmpLen - 1); j > 0; j-- {
c := words[idx+1][j]
lenSb++
if isLetter(rune(c)) {
sb.WriteByte(c)
} else if c == ',' {
break
}
}
tmpLen += lenSb + lenGarbageString
replaceSymbols(&garbageString, config)
multilineMap[strings.ToLower(Reverse(sb.String()))] = garbageString
}
rightString = strings.TrimRight(garbageStrings[0], ",")
} else {
rightString = words[idx+1]
replaceSymbols(&rightString, config)
}
replaceGaps(&rightString, `(?m)('[\S\s]*?')|("[\S\s]*?")`, ",", " ")
paramets := getMapEvent(&rightString)
paramets["date"] = dataEvent
paramets["processNameID"] = file.ProcessNameID
for keyM, valueM := range multilineMap {
paramets[keyM] = valueM
}
paramets["SourceFile"] = file.Path
// Конвертация карты в JSON
empData, err := json.Marshal(paramets)
if err != nil {
deleteFileParametersRedis(conn, file.BlokingID)
logr.WithFields(logr.Fields{
"object": "Data",
"title": "Failure marshal struct",
}).Fatal(err)
}
hash := md5.Sum(empData)
md5String := hex.EncodeToString(hash[:])
// if len(empData) > config.ElasticMaxContentLength {
// empData = empData[:config.ElasticMaxContentLength]
// }
jsonStr := append(empData, "\n"...)
// заголовок bulk
idxName := strings.Replace(indexName, "{event}", strings.ToLower(paramets["event_techlog"]), -1)
mapIndicies[paramets["event_techlog"]] = idxName
meta := []byte(fmt.Sprintf(`{ "index" : { "_index" : "%s","_id" : "%s" } }%s`, idxName, md5String, "\n"))
keyBuffer := paramets["event_techlog"] + "_" + strconv.Itoa(IndexPostfix)
if mapEventsBuffer[keyBuffer] == nil {
mapEventsBuffer[keyBuffer] = new(bytes.Buffer)
}
// заголовок + source события
mapEventsBuffer[keyBuffer].Grow(len(meta) + len(jsonStr))
mapEventsBuffer[keyBuffer].Write(meta)
mapEventsBuffer[keyBuffer].Write(jsonStr)
if int64(mapEventsBuffer[keyBuffer].Len()) >= config.ElasticBulkSize {
IndexPostfix++
}
}
if config.LogLevel == 3 {
logr.WithFields(logr.Fields{
"object": "Data",
"title": "Succeful reading",
}).Infof("Package %d, file %s, start_position: %d, end position: %d", keyInPackage, file.Path, file.LastPosition, currentPosition)
}
// обходим события. Каждому событию соответстует bulk буффер
for keyM, buf := range mapEventsBuffer {
keyMaster := keyM[0:strings.Index(keyM, "_")]
idxName := mapIndicies[keyMaster]
res, err = es.Indices.Exists([]string{idxName})
if err != nil && config.LogLevel == 2 {
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Indices.Exists",
}).Warning(err)
}
res.Body.Close()
if res.StatusCode == 404 {
currentMapping := mapping[strings.ToLower(keyMaster)]
_, err = es.Indices.Create(
idxName,
es.Indices.Create.WithBody(strings.NewReader(currentMapping)),
es.Indices.Create.WithWaitForActiveShards("1"),
es.Indices.Create.WithTimeout(60),
)
if err != nil {
deleteFileParametersRedis(conn, file.BlokingID)
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Cannot create index",
}).Fatal(err)
}
}
// 0.11. отправляем в эластик
res, err = es.Bulk(
bytes.NewReader(buf.Bytes()),
es.Bulk.WithIndex(idxName),
es.Bulk.WithRefresh("false"),
)
if err != nil {
deleteFileParametersRedis(conn, file.BlokingID)
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Failure indexing batch",
}).Fatal(err)
}
// если ошибка запроса - выводим ошибку
if res.IsError() {
if err := json.NewDecoder(res.Body).Decode(&raw); err != nil {
deleteFileParametersRedis(conn, file.BlokingID)
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Failure to to parse response body",
"source": keyMaster,
"file": file.Path,
}).Fatal(err)
} else {
deleteFileParametersRedis(conn, file.BlokingID)
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Request",
}).Errorf("[%d] %s: %s (%s)",
res.StatusCode,
raw["error"].(map[string]interface{})["type"],
raw["error"].(map[string]interface{})["reason"],
file,
)
}
// Успешный ответ может по-прежнему содержать ошибки для определенных документов...
} else {
if err := json.NewDecoder(res.Body).Decode(&blk); err != nil {
deleteFileParametersRedis(conn, file.BlokingID)
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Failure to to parse response body",
"source": keyMaster,
"file": file.Path,
}).Fatal(err)
} else {
for _, d := range blk.Items {
// ... для любых других HTTP статусов > 201 ...
if d.Index.Status > 201 {
deleteFileParametersRedis(conn, file.BlokingID)
// ... и распечатать статус ответа и информацию об ошибке ...
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Request",
}).Errorf("[%d]: %s: %s: %s: %s",
d.Index.Status,
d.Index.Error.Type,
d.Index.Error.Reason,
d.Index.Error.Cause.Type,
d.Index.Error.Cause.Reason,
)
}
}
}
}
// Закрываем тело ответа, чтобы предотвратить достижение предела для горутин или дескрипторов файлов.
res.Body.Close()
}
deleteFileParametersRedis(conn, file.BlokingID) // удаляем ключ
setFileParametersRedis(conn, file.Path, currentPosition) // записываем позицию в базу
}
c <- keyInPackage
}
func initLogging(c *conf) {
year, month, day := time.Now().Date()
if _, err := os.Stat(c.PathLogFile); os.IsNotExist(err) {
os.Mkdir(c.PathLogFile, 2)
}
logFileName := c.PathLogFile + "techLog1C_" + strconv.Itoa(year) + strconv.Itoa(int(month)) + strconv.Itoa(day) + ".json"
fileLog, err := os.OpenFile(logFileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
log.Fatal(err)
}
logr.SetFormatter(&logr.JSONFormatter{})
logr.SetOutput(fileLog)
}
func deleteOldLogFiles(c *conf) {
lifeSpan := c.LogLifeSpan
if lifeSpan == 0 {
lifeSpan = 1
}
files, err := getFilesArray(c.PathLogFile)
if err != nil {
logr.WithFields(logr.Fields{
"object": "Scan log directory",
"title": "Select life span files",
}).Warning(err)
return
}
t := time.Now()
for _, file := range files {
fname := filepath.Base(file.Path)
if !strings.HasPrefix(fname, "techLog1C_") {
// избегает удаления не наших лог файлов, например при неверном указании пользователем нашего лог каталога
continue
}
duration := t.Sub(file.DataCreate)
if (int(duration.Hours()) - 24*c.LogLifeSpan) >= 0 {
if err = os.Remove(file.Path); err != nil {
logr.WithFields(logr.Fields{
"object": "Scan log directory",
"title": "Delete life span files",
}).Warning(err)
}
}
}
}
func getFilesArray(root string) ([]files, error) {
var arrFiles []files
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
arrFiles = append(arrFiles, files{Path: path, Size: info.Size(), DataCreate: info.ModTime()})
}
return nil
})
return arrFiles, err
}
// =======================================================================================
func main() {
defer duration(track())
// считываем конфиг
var config conf
config.getConfig()
// подключаем логи
initLogging(&config)
deleteOldLogFiles(&config)
// maxdop установка
runtime.GOMAXPROCS(config.MaxDop)
// удалим ключи, которые больше не используются
conn, err := redis.Dial("tcp", config.RedisAddr,
redis.DialUsername(config.RedisLogin),
redis.DialPassword(config.RedisPassword),
redis.DialDatabase(config.RedisDatabase),
)
if err != nil {
logr.WithFields(logr.Fields{
"object": "Redis",
"title": "Unable to connect",
}).Fatal(err)
}
defer conn.Close()
// проверим что es доступен
es, err := createElasticsearchClient(&config)
if err != nil {
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Creating the client",
}).Fatal(err)
}
res, err := es.Info()
if err != nil {
logr.WithFields(logr.Fields{
"object": "Elastic",
"title": "Unable to get response",
}).Fatal(err)
}
res.Body.Close()
keys, _ := redis.Strings(conn.Do("KEYS", "*"))
for _, key := range keys {
var currKey string
if strings.Contains(key, "job_") {
currKey = strings.Replace(key, "job_", "", -1)
} else {
currKey = key
}
if _, err := os.Stat(currKey); err != nil {
if os.IsNotExist(err) {
// если файла больше нет - удалим запись из базы
deleteFileParametersRedis(conn, key)
} else {
logr.WithFields(logr.Fields{
"object": "File tech journal",
}).Warning(err)
}
}
}
c := make(chan int)
// получаем файлы логов, сортируем по размеру, определяем в пакеты заданий
arr, err := getFilesArray(config.Path)
if err != nil {
logr.WithFields(logr.Fields{
"object": "Data",
"title": "Failure to scan directory",
}).Error(err)
}
/* проверяем что файлы не заблокированы, ставим блокировку в основном сеансе
считываем позицию файла и сравниваем с текущим размером, если ничего не изменилось -
удаляем такие файлы из массива
*/
var listFiles []*files
separator := string(os.PathSeparator)
for i := 0; i < len(arr); i++ {
// проверим что файла нет в текущей обработке
jobFile := "job_" + arr[i].Path
if getFileParametersRedis(conn, jobFile) == 1 {
continue
}
// получаем последнюю прочитанную позицию из redis
lastPosition := getFileParametersRedis(conn, arr[i].Path)
if lastPosition == arr[i].Size || arr[i].Size < 100 {
continue
}
// получаем дату из имени файла и тип процесса
fileSplitter := strings.Split(arr[i].Path, separator)
lenArray := len(fileSplitter)
if lenArray < 2 {
continue
}
// устанавливаем блокировку на файл
setFileParametersRedis(conn, jobFile, 1)
arr[i].LastPosition = lastPosition
arr[i].FileDate = strings.TrimRight(fileSplitter[lenArray-1], ".log")
arr[i].ProcessNameID = strings.ToLower(fileSplitter[lenArray-2])
arr[i].BlokingID = jobFile
listFiles = append(listFiles, &arr[i])
}
if config.Sorting != 0 {
sort.SliceStable(listFiles, func(i, j int) bool {
if config.Sorting == 1 {
return listFiles[i].Size > listFiles[j].Size
}
return listFiles[i].Size < listFiles[j].Size
})
}
packages := getFilesPacked(listFiles, config.MaxDop)
for keyInPackage, filesInPackage := range packages {
go jobExtractTechLogs(filesInPackage, keyInPackage, &config, c)
}
for i := 0; i < len(packages); i++ {
gopherID := <-c // Получает значение от канала
logr.WithFields(logr.Fields{
"job id": gopherID,
"status": "ok",
}).Info("Job extract tech log 1C")
}
}