-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
285 lines (235 loc) · 6.66 KB
/
Copy pathmain.go
File metadata and controls
285 lines (235 loc) · 6.66 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"gih-ftp/internal/config"
ftpclient "gih-ftp/internal/ftp"
"gih-ftp/internal/gihapi"
"gih-ftp/internal/logger"
"gih-ftp/internal/merger"
sftpclient "gih-ftp/internal/sftp"
)
const (
ExitSuccess = 0
ExitConfigError = 1
ExitFetchError = 2
ExitMergeError = 3
ExitUploadError = 4
ExitPartialError = 5
)
func main() {
// Load configuration (from flags or config file)
cfg, err := config.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "Configuration error: %v\n", err)
fmt.Fprintf(os.Stderr, "\nUsage examples:\n")
fmt.Fprintf(os.Stderr, " Using config file:\n")
fmt.Fprintf(os.Stderr, " %s --config=/etc/gihftp.conf\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, " Using command-line flags:\n")
fmt.Fprintf(os.Stderr, " %s --gih-servers=dns1.example.com,dns2.example.com \\\n", os.Args[0])
fmt.Fprintf(os.Stderr, " --gih-api-port=2035 \\\n")
fmt.Fprintf(os.Stderr, " --ftp-host=127.0.0.1 \\\n")
fmt.Fprintf(os.Stderr, " --ftp-user=upload_user \\\n")
fmt.Fprintf(os.Stderr, " --ftp-log-dir=/var/log/uploads/ \\\n")
fmt.Fprintf(os.Stderr, " --ssh-key=/root/.ssh/id_rsa \\\n")
fmt.Fprintf(os.Stderr, " --work-dir=/tmp/logmerger\n\n")
fmt.Fprintf(os.Stderr, " Password can be provided via FTP_PASSWORD environment variable\n")
os.Exit(ExitConfigError)
}
// Validate configuration
if err := cfg.Validate(); err != nil {
fmt.Fprintf(os.Stderr, "Configuration validation failed: %v\n", err)
os.Exit(ExitConfigError)
}
// Initialize logger
logger.Init(cfg.LogLevel)
logger.Info("GIH-FTP Service Starting",
"version", "2.0.0",
"gih_servers", fmt.Sprintf("%v", cfg.GIHServers),
"ftp_host", cfg.FTPHost,
"work_dir", cfg.WorkDir,
)
// Run main process
exitCode := run(cfg)
if exitCode == ExitSuccess {
logger.Info("GIH-FTP Service completed successfully")
} else {
logger.Error("GIH-FTP Service completed with errors", "exit_code", exitCode)
}
os.Exit(exitCode)
}
func run(cfg *config.Config) int {
startTime := time.Now()
// Create GIH API client
apiClient := gihapi.NewClient(cfg.InsecureSkipVerify)
defer apiClient.Close()
startDate, endDate := getLastWeekRange()
logger.Info("Fetching logs for last week",
"start_date", startDate,
"end_date", endDate,
)
m := merger.New(cfg.WorkDir)
successCount := 0
failureCount := 0
for _, host := range cfg.GIHServers {
err := fetchFromServerWeekly(apiClient, m, host, cfg.GIHAPIPort, startDate, endDate)
if err != nil {
logger.Error("Weekly fetch failed",
"host", host,
"error", err)
failureCount++
} else {
successCount++
}
}
if successCount == 0 {
logger.Error("No successful fetch from any server")
return ExitFetchError
}
stats := m.GetStats()
logger.Info("Weekly merge statistics",
"week_start", startDate,
"week_end", endDate,
"unique_domains", stats["unique_domains"],
"total_requests", stats["total_requests"],
"top_domain", stats["top_domain"],
"top_domain_hits", stats["top_domain_hits"],
)
uploadDate := time.Now().Format("20060102")
filename := fmt.Sprintf("NETINTERNET-GIH-DNS_250k-%s.txt", uploadDate)
outputPath, err := m.SaveToFile(filename)
if err != nil {
logger.Error("Failed to save weekly merged file", "error", err)
return ExitMergeError
}
logger.Info("Weekly merged file created",
"file", outputPath,
"week_start", startDate,
"week_end", endDate,
)
if err := uploadToFTP(cfg, outputPath); err != nil {
logger.Error("FTP upload failed",
"file", outputPath,
"error", err)
return ExitUploadError
}
if cfg.CleanupAfter {
if err := os.Remove(outputPath); err != nil {
logger.Warn("Failed to remove temp file", "file", outputPath)
} else {
logger.Info("Temp file removed", "file", outputPath)
}
}
duration := time.Since(startTime)
logger.Info("Weekly processing completed",
"duration_seconds", duration.Seconds(),
"servers_success", successCount,
"servers_failed", failureCount,
)
if failureCount > 0 {
return ExitPartialError
}
return ExitSuccess
}
func fetchFromServerWeekly(apiClient *gihapi.Client, m *merger.Merger, host, port, startDate, endDate string) error {
logger.Info("Fetching weekly logs from server",
"host", host,
"start_date", startDate,
"end_date", endDate,
)
files, err := apiClient.FetchLogFiles(host, port, startDate, endDate)
if err != nil {
return fmt.Errorf("failed to fetch weekly log list: %w", err)
}
if len(files) == 0 {
logger.Warn("No weekly log files found",
"host", host,
"start_date", startDate,
"end_date", endDate)
return nil
}
logger.Info("Found log files for week",
"host", host,
"file_count", len(files),
)
for _, file := range files {
logger.Debug("Downloading log file",
"host", host,
"filename", file.Filename,
)
content, err := apiClient.DownloadFile(host, port, file.DownloadURL)
if err != nil {
logger.Error("Failed to download log",
"host", host,
"filename", file.Filename,
"error", err)
continue
}
if err := m.AddContent(content); err != nil {
logger.Error("Failed to merge log",
"host", host,
"filename", file.Filename,
"error", err)
continue
}
}
return nil
}
func uploadToSFTP(cfg *config.Config, localPath string) error {
logger.Info("Uploading to SFTP server")
// Create SFTP client
sftpClient := sftpclient.NewClient(
cfg.FTPHost,
cfg.FTPUser,
cfg.FTPPassword,
cfg.SSHKeyPath,
cfg.InsecureSkipVerify,
)
// Build remote path
filename := filepath.Base(localPath)
remotePath := filepath.Join(cfg.FTPLogDir, filename)
// Upload file
if err := sftpClient.Upload(localPath, remotePath); err != nil {
return fmt.Errorf("SFTP upload failed: %w", err)
}
logger.Info("SFTP upload successful",
"local_path", localPath,
"remote_path", remotePath,
)
return nil
}
func uploadToFTP(cfg *config.Config, localPath string) error {
logger.Info("Uploading to FTP server")
ftpClient := ftpclient.NewClient(
normalizeFTPHost(cfg.FTPHost),
cfg.FTPUser,
cfg.FTPPassword,
)
filename := filepath.Base(localPath)
remotePath := filepath.Join(cfg.FTPLogDir, filename)
if err := ftpClient.Upload(localPath, remotePath); err != nil {
return fmt.Errorf("FTP upload failed: %w", err)
}
logger.Info("FTP upload successful",
"local_path", localPath,
"remote_path", remotePath,
)
return nil
}
func normalizeFTPHost(host string) string {
if !strings.Contains(host, ":") {
return host + ":21"
}
return host
}
func getLastWeekRange() (startDate, endDate string) {
now := time.Now()
yesterday := now.AddDate(0, 0, -1)
endDate = yesterday.Format("20060102")
weekAgo := now.AddDate(0, 0, -7)
startDate = weekAgo.Format("20060102")
return startDate, endDate
}