|
| 1 | +package collectors |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "crypto/sha256" |
| 7 | + "encoding/hex" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/SUSE/connect-ng/internal/util" |
| 12 | +) |
| 13 | + |
| 14 | +type LSMOD struct{} |
| 15 | + |
| 16 | +const KernModChecksumFile = "/etc/zypp/chksum-kernmoddata.txt" |
| 17 | +const lsmodTag = "lsmod_data" |
| 18 | + |
| 19 | +func (lsmod LSMOD) run(arch string) (Result, error) { |
| 20 | + modInfo, err := util.Execute([]string{"lsmod"}, nil) |
| 21 | + // Should not happen, but if command fails |
| 22 | + // send nil to SCC to indicate that module data was unavailable. |
| 23 | + if err != nil { |
| 24 | + return Result{lsmodTag: nil}, err |
| 25 | + } |
| 26 | + scanner := bufio.NewScanner(bytes.NewReader(modInfo)) |
| 27 | + |
| 28 | + // Iterate through each line get module name |
| 29 | + modList := make(map[string]bool) |
| 30 | + for scanner.Scan() { |
| 31 | + module := strings.Fields(scanner.Text())[0] |
| 32 | + // skip "Mudule" from the hearder line |
| 33 | + if module != "Module" { |
| 34 | + modList[module] = true // update module list |
| 35 | + } |
| 36 | + } |
| 37 | + // Check for any errors during scanning |
| 38 | + if err := scanner.Err(); err != nil { |
| 39 | + return Result{lsmodTag: nil}, err |
| 40 | + } |
| 41 | + |
| 42 | + // run through mudual list removing duplicates |
| 43 | + var sortedMods []string |
| 44 | + for module := range modList { |
| 45 | + sortedMods = append(sortedMods, module) |
| 46 | + } |
| 47 | + |
| 48 | + // sort the module list, , since the order does not matter |
| 49 | + // and may change over system reboots. |
| 50 | + sort.Strings(sortedMods) |
| 51 | + |
| 52 | + // create new line seperated list of sorted modules |
| 53 | + output := strings.Join(sortedMods, "\n") |
| 54 | + |
| 55 | + byteOutput := []byte(output) |
| 56 | + // get sha256 has a pci data |
| 57 | + mkSha256 := sha256.New() |
| 58 | + mkSha256.Write([]byte(byteOutput)) |
| 59 | + sha256 := mkSha256.Sum(nil) |
| 60 | + modProfileId := hex.EncodeToString(sha256) |
| 61 | + |
| 62 | + oldmodProfileId := util.GetChecksum(KernModChecksumFile) |
| 63 | + if oldmodProfileId != modProfileId { |
| 64 | + if util.UpdateLargeDataIDs { |
| 65 | + util.Debug.Print("updating: ", KernModChecksumFile) |
| 66 | + err = util.PutChecksum(KernModChecksumFile, modProfileId) |
| 67 | + if err != nil { |
| 68 | + return Result{lsmodTag: nil}, err |
| 69 | + } |
| 70 | + } |
| 71 | + var dataBlob util.Profile |
| 72 | + dataBlob.ProfileID = modProfileId |
| 73 | + dataBlob.ProfileData = sortedMods |
| 74 | + return Result{lsmodTag: dataBlob}, nil |
| 75 | + } else { |
| 76 | + var dataBlob util.Profile |
| 77 | + dataBlob.ProfileID = modProfileId |
| 78 | + return Result{lsmodTag: dataBlob}, nil |
| 79 | + } |
| 80 | +} |
0 commit comments